我正在尝试在购物车中添加带有自定义选项的简单产品。
$post = $this->getRequest()->getPost();
$_product = Mage::getModel('catalog/product')->load(8);
$QuoteId= Mage::getModel('checkout/cart_api')->create('default');
$storeId = Mage::app()->getStore()->getId();
$arrProducts = array(
array(
"product_id" => 8,
"qty" => 1,
"options" => array(
'1' => array(
'sku' => 'cheese'
)
)
)
);
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct($_product, $arrProducts);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
但不幸的是产品没有添加到购物车中。 我有错误
" 请指定所需的产品选项"
答案 0 :(得分:2)
我认为你的阵列太多了:
$arrProducts = array(
array(
"product_id" => 8,
"qty" => 1,
"options" => array(
'option_id' => 'option_value'
)
)
);
应该做的工作。
答案 1 :(得分:0)
我已成功使用以下代码,请检查:
$options = array('related_product'=>null,
15=>37,
16=>41,
17=>45,
18=>51,
19=>150000); //Those are my option.
$cart = Mage::getSingleton('checkout/cart');
$cart->init(); // Add a product with custom options
$params = array('product' => $_product->getId(),
'qty' => 1,
'options' => $options
);
$request = new Varien_Object();
$request->setData($params);
$quoteObj->addProduct($_product, $request);