Magento 2.2.0 : Add to cart product error unable to unserialize value
-
20-12-2020 - |
Question
I have create gift card product types and add custom options like this
- Choose Amount:Card value
- select a design:
compose your email :
To : Recipient Name Recipient Email Form : Sender Name Sender Email
- Headline:
- Message:
- Date to send:
- 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.
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