Question

I am facing a weird problem with setAddtionalOptions for checkout. Following is my code to set options using observer.

This is my event.xml file, which calls the observer to set additional options.

<event name="catalog_product_load_after">
      <observer name="set_additional_options" instance="Vendor\ModuleName\Model\Observer\SetAdditionalOptions"/>
</event>

This is observer file Observer/SetAdditionalOptions.php

class SetAdditionalOptions implements ObserverInterface
{

    protected $request;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
        Magento\Framework\Serialize\SerializerInterface $serialize,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->_request = $request;
        $this->timezone = $timezone;
        $this->serialize= $serialize;
        $this ->_logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Check and set information according to your need
        if ($this->_request->getFullActionName() == 'checkout_cart_add') {
            //checking when product is adding to cart

               $additionalOptions[] = array(
                 'label' => "Created at",
                 'value' => $this->timezone->date()->format('Y-m-d H:i:s')
              );
             $observer->getProduct()->addCustomOption('additional_options', $this->serialize->serialize($additionalOptions));

        }
   }
}

The same code is working in 2.3.0 without issue but when I try the same code 2.3.1 it is not allowing me to checkout and also not giving any error in the log.

It is calling and adding created at date in cart item but when I try to checkout, it will redirect to cart page without any issue. If i remove following line then checkout will work fine:

$observer->getProduct()->addCustomOption('additional_options', $this->serialize->serialize($additionalOptions));

Is it a bug or something else?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top