문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

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';


}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top