Magento cart / session data outside magento

11,816

Solution 1

If all you want is the qty why instantiate magento at all. Just set a client side cookie (ie cart_qty) and then read this cookie on your main site header.

Solution 2

I copied your code and tested it on Magento 1.5, 1.6 and 1.7.
I placed the code in a PHP file called test.php in the Magento root directory. This is the code I used:

umask(0);
require_once 'app/Mage.php';
Mage::app();

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

var_dump(array(
    "Mage::getSingleton('checkout/cart')->getItemsCount()" =>
    Mage::getSingleton('checkout/cart')->getItemsCount()
)); // returns number of items (w/o qty)
var_dump(array(
    "Mage::helper('checkout/cart')->getSummaryCount()" =>
        Mage::helper('checkout/cart')->getSummaryCount()
)); // returns number according to configuration
var_dump(array(
    "Mage::getSingleton('customer/session')->isLoggedIn()" =>
    Mage::getSingleton('customer/session')->isLoggedIn()
)); // returns bool true|false

The Magento instances use the local test host names magento15.dev, magento16.dev and magento17.dev.

The I requested the corresponding Magento instances and placed a product in the cart (tested with a configurable and a simple product), and then updated the quantity of the product in the cart.

Between each step I reloaded the test.php file in the browser.
The result is always the same: it works as expected. Each call returns the same values as on the Magento site.

So this means your code is correct, it might be your domain and/or setup (is the browser sending the Magento frontend cookie when you request your test script)?

Share:
11,816
Dave
Author by

Dave

front end developer for Date().currentYear - 1999 years.

Updated on June 17, 2022

Comments

  • Dave
    Dave almost 2 years

    This might get a little confusing as I have tried everything to make this work. All I want is a link in my brand site (domain.com) which shows the qty in my magento 1.5.1 cart (domain.com/shop) I quite easily pulled in product data and navigation blocks but no matter what I do, cart qty is always 0 from outside magento. The main way I tried was just in my brand site to go:

        require_once $_SERVER['DOCUMENT_ROOT'].'/shop/app/Mage.php';
        umask(0);
        Mage::app();        
        Mage::getSingleton('core/session', array('name'=>'frontend'));
        // trying everything 
        Mage::getSingleton('checkout/cart')->getItemsCount(); // returns 0
        Mage::helper('checkout/cart')->getItemsCount(); // returns 0
        Mage::getSingleton('customer/session')->isLoggedIn(); // returns blank
        Mage::helper('checkout/cart')->getCart()->getItemsCount(); // returns 0
        Mage::helper('checkout/cart')->getCart()->getQuote()->getItemsCount(); // returns blank
    

    Then, when none of those worked, I created a template in Magento just to give me the cart qty as a block which returns the block fine but still zero in the cart!

        $block = $this->layout->createBlock('core/template');
        $block->setTemplate('page/html/cartForBrand.phtml');
        return $block->renderView();
    

    and the block in magento is simply

        Mage::getSingleton('core/session', array('name'=>'frontend'));
        $cart = Mage::getModel('checkout/cart')->getQuote()->getData()['items_qty'];
    

    I've seen a lot of people having similar issues: / session_cookie_management, .domain.com cookie_domain(even though that's subdomain specific), I've read and tried everything I could find for 2 days. Constantly deleting session and cache directories and clearing cache and cookies with magento caching disabled.

    This is the first question I've posted on this site after using it for years, I've been stuck on this for 3 days! Pulling my hair out!

  • Dave
    Dave almost 12 years
    Hi Vinai, thanks for doing all that testing. At least now I know I'm not going crazy. I still can't see the problem though and so sick of it, I'm going to go with Gordon's solution
  • Dave
    Dave almost 12 years
    yeah, good solution. I get stuck in a rut when things don't work as they should without thinking about different ways to skin the cat. Thanks
  • Shatir
    Shatir over 10 years
    This would work great when the file is in the root folder of magento. However I believe it wouldn't work when the file is located outside the root directory.
  • Vinai
    Vinai over 10 years
    As long as the file is part of the Magento cookie domain it will work, so it depends on your Webserver setup then the location on the file system.