2
votes

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 ... enter image description here

2
Howdy! stackoverflow.com/questions/13698236/… Notice how this answer uses ->addOption to a quote item and then adds it's options to the 'additional_options' code? This is typically how this would need to be done. If that doesn't resolve it for ya I can take a look :)Mikel Bitson
Mikel, could you please change my code according to your comment ?user3162709

2 Answers

2
votes

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.

0
votes

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