【问题标题】:Magento Programmatic Add To Cart Session IssueMagento 程序化添加到购物车会话问题
【发布时间】:2015-09-02 23:47:28
【问题描述】:

我正在尝试创建一个 ajax 脚本来将可配置项目添加到用户购物车。脚本如下。

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";
}

此脚本通过 POST 获取以下内容。

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

它将商品添加到购物车并完全按照预期输出更新后的数量。

我的问题是前端用户会话购物车永远不会更新。即使 GetItemsQty 输出一个递增的数字,前端中的“我的购物车”链接始终为空。

我什至检查了 $session id 是否与浏览器会话匹配。

【问题讨论】:

    标签: php ajax magento session


    【解决方案1】:

    为了将来参考,这一行

    Mage::app();    
    

    需要包含以下内容

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

    '5' 是商店 id

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 2011-08-21
      • 2015-12-13
      相关资源
      最近更新 更多