0

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?

2
  • Can you try to add log in the file to check whether your file is getting triggered or not or in which situation it is getting failed.
    – nishu
    Commented Jun 25, 2019 at 12:26
  • It is calling and adding created the 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: $observer->getProduct()->addCustomOption('additional_options', $this->serialize->serialize($additionalOptions)); Commented Jun 25, 2019 at 12:28

0

Your Answer

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