views:

274

answers:

2

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  A: 

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.

LinuxGnut
A: 

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.

Edison Software
This "works", but the sessions from my CMS seem to blow it up and cause nothing to work.
LinuxGnut

related questions