add product to cart with custom options

Asif hhh picture Asif hhh · Dec 4, 2012 · Viewed 15.4k times · Source

I have to create a (virtual, simple )product and then add to cart both programmatically, i have done this so far. now i have to set custom options when this product add to cart. but nothing happens . here is my code

  $product = Mage::getModel('catalog/product')->load($product_id);

    $cart = Mage::getModel('checkout/cart');
    $cart->init();


    $params = array(
        'product' => $product->getId(), // This would be $product->getId()
        'qty' => 1,
        'options' => array(
            34 => "value",
            35 => "other value",
            53 => "some other value"
        )
    );      



    try {   
        $cart->addProduct($product, new Varien_Object($params));
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
        $cart->save();
    }
    catch (Exception $ex) {
        echo $ex->getMessage();
    }

Answer

Asif hhh picture Asif hhh · Dec 10, 2012

Here is the code i was come up with success.

$a_options = array(
    'options' => array(
         'label' => 'Choice',
         'value' => $pkg_selected_products,
    )
);

$quoteItem->addOption(new Varien_Object(
    array(
        'product' => $quoteItem->getProduct(),
        'code' => 'additional_options',
        'value' => serialize($a_options)
    )
));

$quote->addItem($quoteItem);
$quote->save();