【发布时间】:2015-06-09 09:03:37
【问题描述】:
我正在尝试将可配置产品添加到购物车,但虽然它没有抛出任何异常,但购物车仍然是空的。
我之前使用过这段代码没有问题,所以我不确定这是否与我正在使用的 Magento 版本有关。
我使用的代码是:
$post = $this->getRequest()->getPost();
$session = Mage::getSingleton('customer/session');
$attr = array_keys($post['sa']);
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
foreach ($post['sa'][$attr[0]] as $optId){
try {
if (abs($post['qty'][$optId]) > 0){
$product = Mage::getModel('catalog/product')->load($post['product']);
$this->getRequest()->setParam('product',$post['product']);
$this->getRequest()->setParam('super_attribute',array(
$attr[0] => $optId
));
$options = array(
"product"=>$post['product'],
"super_attribute"=>array(
$attr[0] => $optId
),
"qty"=>$post['qty'][$optId]
);
$opts = new Varien_Object();
$opts->setData($options);
var_dump($opts);
$cart->addProduct($product, $opts);
$cart->save();
}
} catch (Exception $e){
var_dump($e);
}
}
$cart->save(); // save the cart
$cart->setCartWasUpdated(true);
$pdts = $cart->getAllVisibleItems();
var_dump($pdts);
die("??");
所以我希望得到一个包含 6 个项目的购物车(3 个产品,可配置 + 简单),但是我得到了 null 代替 - 正如您从下面看到的那样,它还显示了 $opts 对象我我想通过:
object(Varien_Object)[507]
protected '_data' =>
array (size=3)
'product' => string '86' (length=2)
'super_attribute' =>
array (size=1)
179 => string '20' (length=2)
'qty' => string '1' (length=1)
protected '_hasDataChanges' => boolean true
protected '_origData' => null
protected '_idFieldName' => null
protected '_isDeleted' => boolean false
protected '_oldFieldsMap' =>
array (size=0)
empty
protected '_syncFieldsMap' =>
array (size=0)
empty
object(Varien_Object)[663]
protected '_data' =>
array (size=3)
'product' => string '86' (length=2)
'super_attribute' =>
array (size=1)
179 => string '19' (length=2)
'qty' => string '2' (length=1)
protected '_hasDataChanges' => boolean true
protected '_origData' => null
protected '_idFieldName' => null
protected '_isDeleted' => boolean false
protected '_oldFieldsMap' =>
array (size=0)
empty
protected '_syncFieldsMap' =>
array (size=0)
empty
object(Varien_Object)[678]
protected '_data' =>
array (size=3)
'product' => string '86' (length=2)
'super_attribute' =>
array (size=1)
179 => string '17' (length=2)
'qty' => string '3' (length=1)
protected '_hasDataChanges' => boolean true
protected '_origData' => null
protected '_idFieldName' => null
protected '_isDeleted' => boolean false
protected '_oldFieldsMap' =>
array (size=0)
empty
protected '_syncFieldsMap' =>
array (size=0)
empty
null
??
任何帮助将不胜感激!
【问题讨论】:
标签: magento cart configurable-product