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.

0

1 Answer 1

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,

1
  • 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, 2016 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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