简体   繁体   English

Magento 2使用自定义选项以编程方式将产品添加到购物车

[英]Magento 2 add product to cart programmatically with custom options

I have created one test script file for add product into cart with custom options . 我创建了一个测试脚本文件,用于将产品添加到带有自定义选项的购物车 I want display selected custom option of product in cart using programmatically. 我希望以编程方式在购物车中显示所选产品的自定义选项。

Please check my below code: 请检查我的以下代码:

$productId = 25;
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
$cart = $objectManager->create('Magento\Checkout\Model\Cart');  
$params = array();      
$params['options[469]'] = 459;
$params['qty'] = 1;
$params['product'] = 25

$cart->addProduct($product, $params);
$cart->save();

Using objectmanager i have created cart and product object. 使用objectmanager我创建了购物车和产品对象。 When i have fired this script in browser, it's show me error: 当我在浏览器中触发此脚本时,它会显示错误:

Magento\\Framework\\Exception\\LocalizedException: Please specify product's required option(s). Magento \\ Framework \\ Exception \\ LocalizedException:请指定产品的必需选项。

I have already passed custom option in params array. 我已经在params数组中传递了自定义选项。 but still it's show error. 但仍然显示错误。

How can i add product into cart with selected custom options ? 如何使用选定的自定义选项将产品添加到购物车中?

Please help me. 请帮我。

Any help would be appreciated. 任何帮助,将不胜感激。

I have got the solution of this problem. 我已经解决了这个问题。 Here is my updated code. 这是我更新的代码。

$productId = 127;
$product = $obj->create('\Magento\Catalog\Model\Product')->load($productId);

$cart = $obj->create('Magento\Checkout\Model\Cart');    
$params = array();      
$options = array();
$params['qty'] = 1;
$params['product'] = 127;

foreach ($product->getOptions() as $o) 
{       
    foreach ($o->getValues() as $value) 
    {
        $options[$value['option_id']] = $value['option_type_id'];

    }           
}

$params['options'] = $options;
$cart->addProduct($product, $params);
$cart->save();

This code is work for me. 这段代码对我有用。

please replace your code : 请替换你的代码:

$productId = 25;
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
$cart = $objectManager->create('Magento\Checkout\Model\Cart');  
$params = array();      
$params['options[469]'] = 459;
$params['qty'] = 1;
$params['product'] = 25

$cart->addProduct($product, $params);
$cart->save();

Replace with 用。。。来代替

$productId = 25;
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
$cart = $objectManager->create('Magento\Checkout\Model\Cart');  
$formKey = $objectManager->create('\Magento\Framework\Data\Form\FormKey')->getFormKey();  
$option = array('469'=>459);

$params = array(
                    'form_key' => $formKey,
                    'product' => $productId, //product Id
                    'qty'   =>1, //quantity of product                
                    'options' => $option
                    );
$cart->addProduct($product, $params);
$cart->save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 马杰托。 如何使用自定义选项以编程方式在购物车中添加简单产品 - Magento. How to add simple product in cart programmatically with custom options 如何以编程方式将带有自定义选项的产品添加到Magento中的购物车? - How to programmatically add a product with custom options to cart in Magento? 以编程方式将产品添加到Magento 2中的购物车 - Programmatically add product to cart in Magento 2 如何将自定义产品添加到magento购物车-请说明这些选项 - How to add a custom product to magento cart - Please explain these options Magento Soap API在购物车中添加具有自定义选项值的产品 - Magento soap api add product with custom options value in shopping cart Magento 2.3.5:使用自定义选项和价格将产品添加到购物车 - Magento 2.3.5: Add product to cart with custom options and price 如何在Magento中以编程方式将产品添加到购物车 - How to add product to cart programmatically in Magento 在Magento中以编程方式将确切1种产品添加到购物车 - Add exactly 1 product to cart programmatically in Magento 添加到购物车中Magento中的产品自定义选项 - Add to cart with product custom option in Magento 添加到购物车中带有magento虚拟产品的自定义选项 - add to cart with custom option for virtual product in magento
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM