我正在尝试在购物车中添加产品,但购物车保持空白.这是我的代码
try{
$product_model = Mage::getSingleton('catalog/product');
// Load product
$_sku = "1-574#AD-B00731";
$my_product_id = $product_model->getIdBySku($_sku);
$my_product = $product_model->load($my_product_id);
$qty_value = 1;
// Add to cart
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($my_product, array('qty' => $qty_value));
$cart->save();
print_r($cart->getItemsQty().PHP_EOL);
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
var_dump("working");
}
catch(Exception $e){
return $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
当我打印$cart->getItemsQty()我的物品数量增加但我的购物车仍然是空的.我认为这Mage::getSingleton('checkout/session')->setCartWasUpdated(true);是不正常的.
有人知道什么不起作用吗?
编辑1:我使用Magento 1.8.0,因此通过url查询因form_key而无效