[英]Magento. How to add simple product in cart programmatically with custom options
I am trying to add my simple product with custom options in cart.我正在尝试在购物车中添加带有自定义选项的简单产品。
$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);
but unfortunately product dont adding in cart.但不幸的是产品没有添加到购物车中。 i have got error
我有错误
" Please specify the product required option(s) " “请指定产品所需的选项”
what i am doing wrong ?我做错了什么? I have two options for product.
我有两种产品选择。 option 1 id is 1 and sku cheese ...
选项 1 id 是 1 和 sku cheese ...
I think you've got a array too much:我认为你的数组太多了:
$arrProducts = array(
array(
"product_id" => 8,
"qty" => 1,
"options" => array(
'option_id' => 'option_value'
)
)
);
Should do the job.应该做这项工作。
I have success with following code please check:我使用以下代码成功,请检查:
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.