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.

1

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,

  • 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: magento.stackexchange.com/questions/1327/… 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

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.