Checking for Magento login on external page

限于喜欢 提交于 2019-12-24 10:47:21

问题


I'm hitting a wall here while trying to access items from Magento on an external page (same server, same domain, etc, etc). I want to see if the user is logged into Magento before showing them certain parts on the site.

Keep in mind that this code exists outside of Magento.

Mage::app("default");
Mage::getSingleton("core/session", array("name" => "frontend"));

if (empty($session)) 
{
  $session = Mage::getSingleton("customer/session");
}

if($session->isLoggedIn()) 
  echo "hi";

$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
echo $cart;

$cart returns 0, where I definitely have products in my cart. isLoggedIn() also returns false. What am I doing wrong here? Is there an option in Magento that I need to turn on or off to be able to access this information outside of Magento?


回答1:


Using the code above, I created a php file in the Magento folder. From there, added the number of items in the cart and whether you were logged in or not to an array and encoded it as json. I used some jquery on my external page to grab the file and pull the data I needed.

Not quite the ideal situation, but it works for now.




回答2:


Are you including Mage.php (which defines getSingleton)?

require_once ($_SERVER['DOCUMENT_ROOT'] . '/app/Mage.php');

What does $session have in it after the getSingleton() call?

print_r ($session);

EDIT: I tried this on my end and was not able to get accurate isLoggedIn() or getItemsCount() data. When I dumped out $session its showing all the fields as 'protected.'

I'm not interested in requiring the user to log in...I merely want to access data from the already logged in session.

Anyone else have any thoughts on this? All the examples out there seem to be pre 1.4.x.




回答3:


require_once "app/Mage.php";

umask(0); 

Mage::app();

//  require_once $_SERVER['DOCUMENT_ROOT'] . "/mage1/app/Mage.php";

// Customer Information

$firstname = "krishana";

$lastname = "singh";

$email = "krish.bhati@gmail.com";

$password = "myverysecretpassword";


// Website and Store details

$websiteId = Mage::app()->getWebsite()->getId();

$store = Mage::app()->getStore();

$customer = Mage::getModel("customer/customer");

$customer->website_id = $websiteId;

$customer->setStore($store);


$mageRunCode = isset ( $_SERVER ['MAGE_RUN_CODE'] ) ? $_SERVER ['MAGE_RUN_CODE'] : '';

$mageRunType = isset ( $_SERVER ['MAGE_RUN_TYPE'] ) ? $_SERVER ['MAGE_RUN_TYPE'] : 'store';

$app = Mage::app ( $mageRunCode, $mageRunType );

Mage::getSingleton('core/session', array('name' => 'frontend')); 

$session = Mage::getSingleton('customer/session');

$session->start();

$customer->loadByEmail($email); 

$customer_id= $customer->getId();


if($customer_id){   

Mage_Core_Model_Session_Abstract_Varien::start();

$session->login($email, $password);

$session->setCustomerAsLoggedIn($session->getCustomer()); 

echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';


}


来源:https://stackoverflow.com/questions/2724334/checking-for-magento-login-on-external-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!