15

I want to add custom option to quoteitem using observer which observer checkoutCartProductAddAfter event and fires after product added to cart.

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{

 $item = $observer->getQuoteItem();  
  $item->addOption(new Varien_Object(
            array(
                    'product' => $item->getProduct(),
                    'label' => 'Free Gifts',
                    'value' => 'Spend $50 and get gift product worth $9.99'
                 )
        ));
    return;

}

My observer is working but i am not able to add custom option to added product. please provide help to add custom option using observer to just added product.

Fabian Schmengler
  • 65,791
  • 25
  • 187
  • 421
Keyur Patel
  • 392
  • 1
  • 5
  • 20
  • also provide me to change product's custom price in observer. i want to add custom price to zero. thanks in advance – Keyur Patel Mar 13 '13 at 11:39

2 Answers2

19

@Tim gave a talk about this issue on the weekend: https://docs.google.com/presentation/d/1efPznQSVTrT1HAD1xQvCPC-Tgvr8jYok4X7ZEJhm9jE/edit

What you want is Method 2: Add Following Event in Config.xml

<sales_quote_collect_totals_before>
<observers>
<hackathon_presentation>
<type>singleton</type>
<class>modulename/observer</class>
<method>salesQuoteAddressCollectTotalsBefore</method>
</hackathon_presentation>
</observers>
</sales_quote_collect_totals_before>

In Observer.php add following Method

   public function salesQuoteAddressCollectTotalsBefore($observer)
    {
        $quote = $observer->getQuote();
        $quote_items = $quote->getItemsCollection();
        foreach ($quote_items as $item) {
            $additionalOptions = array(
                array(
                    'code'  => 'my_code',
                    'label' => 'This text is displayed through additional options',
                    'value' => 'ID is ' . $item->getProductId() . ' and SKU is ' . $item->getSku()
                )
            );
            $item->addOption(
                array(
                     'code'  => 'additional_options',
                     'value' => serialize($additionalOptions),
                )
            );
        }
    }

Here is more about this topic:

https://stackoverflow.com/questions/9334115/magento-change-custom-option-value-before-adding-it-to-cart/9344336#9344336

and more:

https://stackoverflow.com/questions/9412074/magento-quote-order-product-item-attribute-based-on-user-input/9496266#9496266

Fabian Blechschmidt
  • 35,388
  • 8
  • 75
  • 182
  • 1
    First: Please use comments, instead of new answers. And then insert a check for your product in the loop, so only your desired product gets the option. – Fabian Blechschmidt Mar 15 '13 at 09:52
  • i am not able to add custom option in cart page. It give the blank page after product add to cart. – Milople Inc Nov 21 '13 at 07:41
  • 1
    Please have a look on this great debugging answer and comment back: http://magento.stackexchange.com/a/429/217 – Fabian Blechschmidt Nov 21 '13 at 10:27
  • i want to add comment in my cart page and so want to display in order is it possible using custom option or it required any thing else i using checkout_cart_product_add_after . please give any suggestion. – Milople Inc Nov 21 '13 at 10:35
  • 1
    This is possible with the above described steps – Fabian Blechschmidt Nov 21 '13 at 12:09
  • How i can get the posted the value in sales_quote_collect_totals_before observer , i have tried this Mage::app()->getRequest()->getParam('XX'); but not getting same this in checkout_cart_product_before observer i am getting . i have to set the posted data to custom option – Milople Inc Nov 21 '13 at 12:17
  • How to do it when we have two item in cart because it update the option with last value . – Milople Inc Feb 12 '14 at 07:22
  • @GartHuff foreach iterates over all the quoteItems, therefore you have to check what quote item you have by hand. Whatever condition you want to check, if this condition is met, you add the option – Fabian Blechschmidt Feb 12 '14 at 12:53
  • 2
    Thank you for suggestion When i do add to cart i got two item because i am using the configurable product and i have solved the problem by updating the last quote item . – Milople Inc Feb 17 '14 at 06:49
  • It shows error for configurable products. – SIBHI S Feb 08 '15 at 17:52
  • i dont know why people rejected edit when it is useful to update the link because there is already not found in question. – Bhupendra Jadeja Jul 10 '15 at 12:12
  • 1
    Me neither, but I updated the link. Thanks! – Fabian Blechschmidt Jul 10 '15 at 12:20
3

The appropiate event to add custom options on the fly is catalog_product_type_prepare_full_options, which is triggered just before the product with its custom options is converted to a quote item.

Should the own buyRequest data have effect on product attributes or options, an observer on the event catalog_product_type_prepare_{$processMode}_options is a good choice, where $processMode is the validation mode and can be „full“ or „lite“. The „full“ mode is used when a product is regularly added to the cart and validates if all required options are set and the whole configuration is valid. In the „lite“ mode only options contained in the request are validated, it is used when adding a product to the wishlist, but also possible when creating an order from the backend. To process the data in any case you can register the observer for both events. Should there be validation you should differentiate the events of course.

The events are triggered in Mage_Catalog_Model_Product_Type_Abstract::_prepareOptions() and the following parameters are available:

  • transport: Transport object for all custom options (but no other options, for example bundle options), so you can change them in the observer. transport->options is an array in the form option_id => option_value. Attention, transport itself is a stdClass object, not an instance of Varien_Object, as you might expect. So there are no getter and setter methods for transport->options.
  • buy_request: The buyRequest object, you can read it here and still modify it as well.
  • product: The product that will be converted to a quote item later on. Here you can manipulate attributes or add them dynamically. But you still need to consider them in the conversion process. The event used for this, sales_quote_product_add_after, is triggered later only.

Source: The Magento buyRequest Object – A Reference

So an observer might look like this:

public function addCustomOption(Varien_Event_Observer $observer)
{
    $transport = $observer->getTransport();
    if (this_item_should_be_free()) { // implement your condition here
        $transport->options['Free Gifts'] = 'Spend $50 and get gift product worth $9.99';
    }
}

You cannot set a price for this dynamically added custom option, but you can change the price of the quote item using a second observer for catalog_product_get_final_price like this:

public function adjustFinalPrice($observer) {
$product = $observer-&gt;getProduct();
// Set price to &quot;0&quot; if custom option &quot;Free Gift&quot; has been set
if ($product-&gt;getCustomOption('Free Gift')) {
    $product-&gt;setFinalPrice(0);
}

}

Fabian Schmengler
  • 65,791
  • 25
  • 187
  • 421