简体   繁体   English

Magento编程添加到购物车会议期

[英]Magento Programmatic Add To Cart Session Issue

I'm trying to create an ajax script to add configurable items to the user cart. 我正在尝试创建一个Ajax脚本以将可配置的项目添加到用户购物车。 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. 该脚本通过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. 即使GetItemsQty输出一个递增的数字,前端中的“我的购物车”链接始终为空。

I've even checked that $session id matches the browser session. 我什至检查了$ session id是否与浏览器会话匹配。

For future reference, this line 供以后参考,此行

Mage::app();    

needed to include the following 需要包括以下内容

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

'5' being the store id “ 5”是商店ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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