Checking for Magento login on external page

2019-08-10 20:22发布

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?

3条回答
Animai°情兽
2楼-- · 2019-08-10 20:44

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.

查看更多
SAY GOODBYE
3楼-- · 2019-08-10 20:45
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';


}
查看更多
Rolldiameter
4楼-- · 2019-08-10 21:00

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.

查看更多
登录 后发表回答