简体   繁体   中英

Magento Programmatic Add To Cart Session Issue

I'm trying to create an ajax script to add configurable items to the user cart. Script is below.

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

error_reporting(E_ALL);
ini_set('display_errors', 1);

$json = json_decode($_POST['json'],true);

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

if ( $_POST['form_key'] == $session->getFormKey() ) {
    $cart = Mage::getSingleton('checkout/cart')->init();
    $_product = Mage::getModel('catalog/product')->load($json['product']);
    $cart->addProduct($_product,$json)->save();
    $session->setCartWasUpdated(true);
    echo $cart->getItemsQty();
} else {
    echo "Access Denied";
}

This script gets the following through POST.

"json" = "{"super_attribute": {"76":"10","576":"82"},"qty":"1","product":"532"}"
"form_key" = "**form_key**"

It adds the item to the cart and outputs the updated quantity exactly as expected.

My problem is the frontend user session cart is never updated. Even though GetItemsQty outputs an incremented number the 'My Cart' link in the frontend is always empty.

I've even checked that $session id matches the browser session.

For future reference, this line

Mage::app();    

needed to include the following

Mage::app(5)->loadArea('frontend');

'5' being the store id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM