Magento. How to add simple product in cart programmatically with custom options

喜夏-厌秋 提交于 2019-12-12 17:01:54

问题


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:


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.




回答2:


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); 


来源:https://stackoverflow.com/questions/33509436/magento-how-to-add-simple-product-in-cart-programmatically-with-custom-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!