Add option(s) to quote item object at order processing step

Hi guys, how are you? Hope all of you are doing well. I have been backed after long time and have decided to share few of my recent experiences.

For a recent project I had to create a custom order processing module in magento. I got help from a post of magento forum ( http://www.magentocommerce.com/boards/viewthread/28426/). That was nice and very helpful for me. And it saved my time as well. So my heartiest thanks to all of the guys who have been contributed in that forum post.

But I faced problem to add option(s) with order item. I found a solution to add option(s) to quote object. But using this you can order only single item at a time. Below is the code snap.

$option = array('options'=>array(
              "option_id1" => 'option_value1',
              "option_id2" => 'option_value2'
             ));

$request = new Varien_Object();
 $request->setData($option);
$quoteObj->addProduct($productObj,$request);

To order multiple items at a time you have to add option(s) with quote item object. Here is the code snap for your help.

$quoteObj=Mage::getModel('sales/quote')->assignCustomer($customerObj);
$productModel=Mage::getModel('catalog/product');

foreach($products as $product) {
	$productObj = $productModel->load($productId);
	$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);

	$quoteItem->addOption(new Varien_Object(
                   	array(
                      		'product' => $quoteItem->getProduct(),
                       		'code' => 'option_ids',
                       		'value' => $option_id  // 45,46,55
               	         )
               	));

	$quoteItem->addOption(new Varien_Object(
                    	array(
                       		'product' => $quoteItem->getProduct(),
                       		'code' => 'option_'.$option_id,   //45
                       		'value' => $option_value          // ‘White’
             	         )
   		));

        $quoteItem->addOption(new Varien_Object(
                    	array(
                       		'product' => $quoteItem->getProduct(),
                       		'code' => 'option_'.$option_id,   //46
                       		'value' => $option_value         // ‘Large’
               	        )
   		));

	$quoteItem->setQuote($quoteObj);
	$quoteItem->setQty($Quantity);
	$quoteObj->addItem($quoteItem);
}
$quoteObj->collectTotals();
$quoteObj->save();

This code snap has been worked so far. For more details and any kind of helps contact with me.

Advertisement

3 Responses to Add option(s) to quote item object at order processing step

  1. Hey, thank you for that pice of code safed me a lot of time! Sad to say that this is not workling correctly for me :)
    I have the problem that only the first of all options (after ‘code’ => ‘option_ids’) is set, the options follwed will be ignored. Its only one simpleproduct with 3 options, ids are correct and no error appears -> Product with Option “Diverses….” was successfully added.

    Code locks as follwed:

    $simpleProduct = Mage::getModel(‘catalog/product’)->load(2272);
    // load the simple product with options
    $quoteItem = Mage::getModel(‘sales/quote_item’)->setProduct($simpleProduct);

    /* Options are type of textarea..*/
    $options = array(
    new Varien_Object(array(‘product’ => $quoteItem->getProduct(),’code’ => ‘option_ids’,'value’ => 141,142,143)),
    new Varien_Object(array(‘product’ => $quoteItem->getProduct(),’code’ => ‘option_141′,’value’ => ‘Diverses…’)),
    new Varien_Object(array(‘product’ => $quoteItem->getProduct(),’code’ => ‘option_142′,’value’ => ‘Bauteile…’)),
    new Varien_Object(array(‘product’ => $quoteItem->getProduct(),’code’ => ‘option_143′,’value’ => ‘Lackierung…’))
    );
    // Problem: only the first Option will be set..
    $quoteItem->setOptions($options); // run the array and do $this->addOption

    $quote = Mage::getSingleton(‘checkout/session’)->getQuote();
    $quoteItem->setQuote($quote);
    $quoteItem->setQty( 1 );
    //$quoteItem->setCustomPrice($this->totals);
    //$quoteItem->setOriginalCustomPrice($this->totals);

    $quote->addItem($quoteItem);

    $quote->collectTotals();
    $quote->save();

    Magento version is 1.4.1.1

    thanks in advance!

  2. Moinul Al-Mamun

    Sorry for late reply. Here is a code snap of my production system.

    $quoteItem->addOption(new Varien_Object(
                    array(
                        'product' => $quoteItem->getProduct(),
                        'code' => 'option_ids',
                        'value' => "$option_id1,$option_id2"
                    )
                ));
    
    $quoteItem->addOption(new Varien_Object(
                        	array(
                            	'product' => $quoteItem->getProduct(),
                            	'code' => 'option_'.$option_id1,
                            	'value' => "$apply"
            	            	)
       		             	));
    
    $quoteItem->addOption(new Varien_Object(
                        	array(
                            	'product' => $quoteItem->getProduct(),
                            	'code' => 'option_'.$option_id2,
                            	'value' => "$mobileno"
            	            	)
       		             	));
    
  3. Hey, nice code, this exactly what im looking for, but where to put it?
    Still a noob :) ) i think

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s