使用自定义选项将产品添加到购物车 [英] add product to cart with custom options
本文介绍了使用自定义选项将产品添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须创建一个(虚拟的,简单的)产品,然后以编程方式将其添加到购物车中,到目前为止,我已经做到了.现在,当此产品添加到购物车时,我必须设置自定义选项.但是什么也没发生.这是我的代码
I have to create a (virtual, simple )product and then add to cart both programmatically, i have done this so far. now i have to set custom options when this product add to cart. but nothing happens . here is my code
$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();
}
推荐答案
这是我成功编写的代码.
Here is the code i was come up with success.
$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();
这篇关于使用自定义选项将产品添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文