如何以编程方式将带有自定义选项的产品添加到Magento中的购物车? [英] How to programmatically add a product with custom options to cart in Magento?
问题描述
自定义选项是必需的,它是一个下拉类型: 标题是捐赠 价格是1 价格类型是固定的
Custom options will be required and it's a dropdown type: title is donation price is 1 price type is fixed
现在,它将产品添加到购物车中,但没有自定义选项.这是自定义选项的屏幕截图.
Right now , it's adding that product to the cart but without custom options. Here is a screenshot of custom options.
$id = '67'; // Replace id with your product id
$qty = '1'; // Replace qty with your qty
$_product = Mage::getModel('catalog/product')->load($id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
'product'=>$id,
'qty' => $qty,
'options' => array (
183 => array(
array(
'price'=> 1.00,
'title'=>'$1.00 Donation',
'price_type' => 'fixed',
'sku' => '',
'sort_order' => 10
)
)
)
);
$request = new Varien_Object();
$request->setData($params);
$cart->addProduct($_product, $request );
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
该产品变量转储中包含什么
What's in that product variable dump
[_resourceName:protected] => catalog/product_option_value
[_resource:protected] =>
[_resourceCollectionName:protected] => catalog/product_option_value_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[option_type_id] => 641
[option_id] => 183
[sku] =>
[sort_order] => 10
[default_title] => $1.00 Donation
[store_title] =>
[title] => $1.00 Donation
[default_price] => 1.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 1.0000
[price_type] => fixed
推荐答案
如果您已经有报价ID,则使用
If you have already a quote id then used
在下面..
要获取选项ID和选项值,请检查以下链接: Magento-从以下位置获取产品选项$ item
For getting option id and option value check this link: Magento - Get Product options from $item
在这里您需要获取产品下拉选项值 如果您需要创建新报价,则可以在
Here you need to fetch product drop down option value If you need to create new quote then used add below
$QuoteId= Mage::getModel('checkout/cart_api')->create('default');
此处default
是store code
.
如果您已经有报价单ID,则无需添加大写代码
Here default
is store code
.
IF you have already a quote id then not need to add upper code
$arrProducts = array(
array(
"product_id" => $productId,
"qty" => 5,
"options" => array(
$optionId => $optinValueId
)
)
);
Mage::getModel('checkout/cart_product_api')->add($QuoteId,$arrProducts,$storeId);
这篇关于如何以编程方式将带有自定义选项的产品添加到Magento中的购物车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!