Tuesday, December 8, 2015

Add new custom option using Observer in cart while add to cart in Magento



I am saving time to all who is viewing this article.

one event was found by me which help you to add custom option

app\code\core\Mage\Catalog\Model\Product\Type\Abstract.php Line number: 541

   
 $eventName = sprintf('catalog_product_type_prepare_%s_options', $processMode);  
     Mage::dispatchEvent($eventName, array(  
       'transport'  => $transport,  
       'buy_request' => $buyRequest,  
       'product' => $product  
     ));  
ok let's do practical.

app/code/local/Test/Module1/etc/config.xml
config.xml

 <config>  
   <global>  
     <events>  
       <catalog_product_type_prepare_full_options>  
           <observers>  
             <test_module_catalog_product_type_prepare_full_options>  
                 <type>singleton</type>  
                 <class>module1/observer</class>  
                 <method>addTestInfo</method>  
             </test_module_catalog_product_type_prepare_full_options>  
          </observers>  
        </catalog_product_type_prepare_full_options>  
     </events>  
   </global>  
 </config>  

app/code/local/Test/Module1/Model/Observer.php



  public function addTestInfo(Varien_Event_Observer $observer)  
  {  
     $product = $observer->getProduct();  
     $additionalOptions = array();  
     $additionalOptions[] = array(  
       'label'=> Mage::helper('core')->__('Label1');  
       'value' => Mage::helper('core')->__('value1');  
     $observer->getProduct()  
     ->addCustomOption('additional_options',serialize($additionalOptions));  
    return $this;   
  }  


After Follwing this code,you will see that label1 and value1 text set in cart after product name
Any dout or query about this post feel free ask me ?

1 comment:

  1. Hi, this works when I add the product to cart, but it doesn't when I try to add custom option to the product when the product is already in the cart. Is there a way to achieve this?

    ReplyDelete

Translate