简体   繁体   English

马杰托。 如何使用自定义选项以编程方式在购物车中添加简单产品

[英]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.

相关问题 Magento。 如何以编程方式为自定义选项(复选框)设置数据并在购物车中添加简单产品? - Magento. How to set data for custom options(checkbox) programatically and add simple product in cart? Magento 2使用自定义选项以编程方式将产品添加到购物车 - Magento 2 add product to cart programmatically with custom options 如何以编程方式将带有自定义选项的产品添加到Magento中的购物车? - How to programmatically add a product with custom options to cart in Magento? 如何将自定义产品添加到magento购物车-请说明这些选项 - How to add a custom product to magento cart - Please explain these options 如何在Magento中以编程方式将产品添加到购物车 - How to add product to cart programmatically in Magento 以编程方式将产品添加到Magento 2中的购物车 - Programmatically add product to cart in Magento 2 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中以编程方式将确切1种产品添加到购物车 - Add exactly 1 product to cart programmatically in Magento Magento。 可配置产品-选择2个或更多选项(多个选项) - Magento. Configurable product - choose 2 or more options(multiple options)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM