Question

I have create gift card product types and add custom options like this

  1. Choose Amount:Card value
  2. select a design:
  3. compose your email :

       To :
    
         Recipient Name
         Recipient Email
    
       Form :
    
         Sender Name
         Sender Email
    
  4. Headline:
  5. Message:
  6. Date to send:
  7. Time Zone

I added product into add to cart But, product is not update mini cart and checkout/cart page display error like this :

Exception #0 (InvalidArgumentException): Unable to unserialize value. #0 vendor/magento/module-catalog/Helper/Product/Configuration.php(118): Magento\Framework \Serialize\Serializer\Json->unserialize.

How to solve this type issue?

I tried the answers from "Magento 2.2 error: Unable to unserialize value" but this is not working.


enter image description here

Was it helpful?

Solution

Now i have got the solution.

app/code/Vendor/Module/Helper/Data.php

    public function getUnserializeData($data)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $version = $objectManager->get('Magento\Framework\App\ProductMetadataInterface')->getVersion();
        if($version >= '2.2.0'){
            $returnData = $objectManager->get('Magento\Framework\Serialize\SerializerInterface')->unserialize($data);
        }
        else{

            $returnData = (array) unserialize($data);
        }

        return $returnData;     
    }
    public function getSerializeData($data)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $version = $objectManager->get('Magento\Framework\App\ProductMetadataInterface')->getVersion();
        if($version >= '2.2.0'){
            $returnData = $objectManager->get('Magento\Framework\Serialize\SerializerInterface')->serialize($data);
        }
        else{

            $returnData = serialize($data);
        }
        return $returnData;     
    }

app/code/Vendor/Module/Observer/CheckoutCartProductAddAfterObserver.php

        $additionalOptions = [];
        if ($additionalOption = $item->getOptionByCode('additional_options')) {

            $additionalOptions = $this->_helper->getUnserializeData($additionalOption->getValue());
        }


       if (count($additionalOptions) > 0) {
            $item->addOption([
                'code' => 'additional_options',
                'value' => $this->_helper->getSerializeData($additionalOptions)
            ]);
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top