0

I want to create new custom option for quote item in my observer. How can I do that? Here is my code:

public function execute(\Magento\Framework\Event\Observer $observer) {
    $item = $observer->getEvent()->getData('quote_item');
    $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        $price = '100'; //set your price here
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        $item->getProduct()->setIsSuperMode(true);
}

I am setting custom price but I also need to set some custom options. I have created observer on checkout_cart_product_add_after event.

Mohit Kumar Arora
  • 9,951
  • 7
  • 27
  • 55
Atharuddin
  • 35
  • 7

1 Answers1

2

You can use catalog_product_type_prepare_full_options event if you want to set custom option

In this event you can easily get $product object by using

$product = $observer->getEvent()->getProduct();

set your custom option by

$infoOptions[] = array(
                            'label' => "YOUR_LABEL",
                            'value' => "YOUR_VALUE",
                        );

Add this custom option by

 $product->addCustomOption('additional_options', serialize($infoOptions));       

just it,

Keyur Shah
  • 18,046
  • 6
  • 66
  • 80
  • thanks for your comment I will definitely give a try to your method and mark your answer correct if it works. I solved my problem with by creating an observer on sales_quote_collect_totals_before event. source: http://magento.stackexchange.com/questions/1327/creating-custom-option-and-prize-to-just-added-product-to-cart-using-observer

    Only trick was the declaration of observer... In magento 2 I declared it in my event.xml file.

    – Atharuddin Jul 13 '16 at 11:55