3
<?php

namespace Ecomsolver\Measurement\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class SetAdditionalOptions implements ObserverInterface
{
    /**
     * @var RequestInterface
     */
    protected $_request;

    /**
     * @param RequestInterface $request
     */
    public function __construct(
        RequestInterface $request
    ) {
        $this->_request = $request;
    }

    /**
     * @param \Magento\Framework\Event\Observer $observer
     */
    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
            $product = $observer->getProduct();
            $additionalOptions = [];
            $additionalOptions[] = array(
                'label' => "Some Label",
                'value' => "good product",
            );
            $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
        }
    }
}

I am using the above code for adding custom option when product add to cart in magento 2.2.5 but everytime I am getting the below error:

main.CRITICAL: Unable to unserialize value. [] []

how to fix this ?

  • 1
    try ditching the serialize call. I think this is done automatically. – Marius Aug 20 '18 at 9:09
  • 1
    try this : $additionalOptions = []; $additionalOptions["some_label"] = array( 'label' => "Some Label", 'value' => "good product", ); – Abdul Aug 20 '18 at 9:12
  • how i use this code – Roshan Yadav Aug 20 '18 at 9:25
0

For Security magento from 2.2 version, Magento suggesting to not use serialize. https://devdocs.magento.com/guides/v2.2/extension-dev-guide/framework/serializer.html

So,serialize() cannot use, you have to use

Magento\Framework\Serialize\Serializer\Json

use Magento\Framework\Serialize\Serialize\Json;

...

/**
 * @var SerializerInterface
 */
proteccted $serializer;

...

public function __construct(Json $serializer) {
  $this->serializer = $serializer;
}

use $this->serializer->serialize() instead of serialize().

So you have to changes

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

to

$observer->getProduct()->addCustomOption('additional_options', $this->serializer->serialize($additionalOptions));
  • not success i have update the code as you told – Roshan Yadav Aug 20 '18 at 9:22
  • [2018-08-20 09:20:44] main.CRITICAL: Notice: Undefined property: Ecomsolver\Measurement\Observer\SetAdditionalOptions::$serializer in D:\xampp\htdocs\store\app\code\Ecomsolver\Measurement\Observer\SetAdditionalOptions.php on line 36 {"exception":"[object] (Exception(code: 0): Notice: Undefined property: Ecomsolver\\Measurement\\Observer\\SetAdditionalOptions::$serializer in D:\\xampp\\htdocs\\store\\app\\code\\Ecomsolver\\Measurement\\Observer\\SetAdditionalOptions.php on line 36 at D:\\xampp\\htdocs\\store\\vendor\\magento\\framework\\App\\ErrorHandler.php:61)"} [] – Roshan Yadav Aug 20 '18 at 9:22
  • getting this error after change the code – Roshan Yadav Aug 20 '18 at 9:23
  • You have to do Di compile – Amit Bera Aug 20 '18 at 9:24
  • yes i do di compile or not getting any error – Roshan Yadav Aug 20 '18 at 9:32
0

Try the below code:

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
        $item = $observer->getQuoteItem();
        $product = $observer->getProduct();
        $additionalOptions = array();
        if ($additionalOption = $item->getOptionByCode('additional_options')) {
            $additionalOptions = (array) json_decode($additionalOption->getValue());
        }
        $additionalOptions = [];
        $additionalOptions[] = array(
            'label' => "Some Label",
            'value' => "good product",
        );
        if(count($additionalOptions) > 0) {
            $item->addOption([
                'product_id' => $item->getProductId(),
                'code' => 'additional_options',
                'value' => json_encode($additionalOptions)
            ]);
        }
    }
}

Also use checkout_cart_product_add_after event for the above.

  • 1
    If it's working for you then click the green tick so that in future it helps for others. – Sukumar Gorai Aug 21 '18 at 5:42

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.