Asi*_*hhh 4 product magento cart
我必须创建一个(虚拟的,简单的)产品,然后以编程方式添加到购物车,到目前为止我已经这样做了.现在我必须在此产品添加到购物车时设置自定义选项.但没有任何反应.这是我的代码
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
'product' => $product->getId(), // This would be $product->getId()
'qty' => 1,
'options' => array(
34 => "value",
35 => "other value",
53 => "some other value"
)
);
try {
$cart->addProduct($product, new Varien_Object($params));
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$cart->save();
}
catch (Exception $ex) {
echo $ex->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
这是我成功的代码.
$a_options = array(
'options' => array(
'label' => 'Choice',
'value' => $pkg_selected_products,
)
);
$quoteItem->addOption(new Varien_Object(
array(
'product' => $quoteItem->getProduct(),
'code' => 'additional_options',
'value' => serialize($a_options)
)
));
$quote->addItem($quoteItem);
$quote->save();
Run Code Online (Sandbox Code Playgroud)