2015-09-02 23 views
0

我正在嘗試創建一個ajax腳本來向用戶購物車添加可配置的項目。腳本在下面。Magento編程添加到購物車會話問題

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是否與瀏覽器會話匹配。

+0

請參見下面的答案需要 – theunknown

回答

0

以供將來參考,這條線

Mage::app();  

包括以下

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

'5' 是店鋪ID