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.

  • 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
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

  • 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 9:52
  • 1
    Please have a look on this great debugging answer and comment back: magento.stackexchange.com/a/429/217 – Fabian Blechschmidt Nov 21 '13 at 10:27
  • 1
    This is possible with the above described steps – Fabian Blechschmidt Nov 21 '13 at 12:09
  • 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 6:49
  • 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->getProduct();
    // Set price to "0" if custom option "Free Gift" has been set
    if ($product->getCustomOption('Free Gift')) {
        $product->setFinalPrice(0);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.