Categories: How ToMagento 2

Solved: “Unable to Unserialize Value” in Magento 2.2

With the launch of Magento 2.2 version, it introduced major change in terms of serialization. It introduces json serialization instead of regular one. There are many tables in database which includes several fields using serialized values, for example: catalogrules tables. While dealing with products, orders and customers in Magento 2.2, when the data is commanded to unserialize, it flashes an error named “Unable to Unserialize Value”. We were working on a client’s project and while adding products to cart, it showed the same error. The error is already available in Magento 2.2.X versions, you may get the same while dealing with order, product or customers data and voila, we nailed the solution for this “Unable to Unserialize Value” in Magento 2.2.

To solve this, go to \vendor\magento\framework\Serialize\Serializer\json.php and replace the below function in the file.

public function unserialize($string)
{
    if($this->is_serialized($string))
    {
        $string = $this->serialize($string);
    }
    $result = json_decode($string, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
         throw new \InvalidArgumentException('Unable to unserialize value.');

    }
    return $result;
}

function is_serialized($value, &$result = null)
{
    // Bit of a give away this one
    if (!is_string($value))
    {
        return false;
    }
    // Serialized false, return true. unserialize() returns false on an
    // invalid string or it could return false if the string is serialized
    // false, eliminate that possibility.
    if ($value === 'b:0;')
    {
        $result = false;
        return true;
    }
    $length = strlen($value);
    $end    = '';
    switch ($value[0])
    {
        case 's':
            if ($value[$length - 2] !== '"')
            {
                return false;
            }
        case 'b':
        case 'i':
        case 'd':
            // This looks odd but it is quicker than isset()ing
            $end .= ';';
        case 'a':
        case 'O':
            $end .= '}';
            if ($value[1] !== ':')
            {
                return false;
            }
            switch ($value[2])
            {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    break;
                default:
                    return false;
            }
        case 'N':
            $end .= ';';
            if ($value[$length - 1] !== $end[0])
            {
                return false;
            }
            break;
        default:
            return false;
    }
    if (($result = @unserialize($value)) === false)
    {
        $result = null;
        return false;
    }
    return true;
}

Implementing the above code will definitely unserialize the old values and serializes them back using Json. Let me know through commenting if you stuck somewhere and need help regarding this code. I’ll be glad to help.

Click to rate this post!
[Total: 32 Average: 3.9]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • Yes, This is working solution. Thank You.
    Yes, We also can override this file. I have done same task with overriding this file in my custom module which is working in local and live as well.
    Just put following line in di.xml inside etc directory

    And inside Namespace\ModuleName\Serialize\Serializer Directory: file Json.php

    is_serialized($string))
    {
    $string = $this->serialize($string);
    }
    $result = json_decode($string, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
    throw new \InvalidArgumentException('Unable to unserialize value.');

    }
    return $result;
    }

    function is_serialized($value, &$result = null)
    {
    // Bit of a give away this one
    if (!is_string($value))
    {
    return false;
    }
    // Serialized false, return true. unserialize() returns false on an
    // invalid string or it could return false if the string is serialized
    // false, eliminate that possibility.
    if ($value === 'b:0;')
    {
    $result = false;
    return true;
    }
    $length = strlen($value);
    $end = '';
    switch ($value[0])
    {
    case 's':
    if ($value[$length - 2] !== '"')
    {
    return false;
    }
    case 'b':
    case 'i':
    case 'd':
    // This looks odd but it is quicker than isset()ing
    $end .= ';';
    case 'a':
    case 'O':
    $end .= '}';
    if ($value[1] !== ':')
    {
    return false;
    }
    switch ($value[2])
    {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    break;
    default:
    return false;
    }
    case 'N':
    $end .= ';';
    if ($value[$length - 1] !== $end[0])
    {
    return false;
    }
    break;
    default:
    return false;
    }
    if (($result = @unserialize($value)) === false)
    {
    $result = null;
    return false;
    }
    return true;
    }
    }

    Works perfectly

  • Yes, This is working solution. Thank You.
    Yes, We also can override this file. I have done same task with overriding this file in my custom module which is working in local and live as well.
    Just put following line in di.xml inside etc directory

    And inside NamespaceModuleNameSerializeSerializer Directory: file Json.php

    is_serialized($string))
    {
    $string = $this->serialize($string);
    }
    $result = json_decode($string, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
    throw new InvalidArgumentException('Unable to unserialize value.');

    }
    return $result;
    }

    function is_serialized($value, &$result = null)
    {
    // Bit of a give away this one
    if (!is_string($value))
    {
    return false;
    }
    // Serialized false, return true. unserialize() returns false on an
    // invalid string or it could return false if the string is serialized
    // false, eliminate that possibility.
    if ($value === 'b:0;')
    {
    $result = false;
    return true;
    }
    $length = strlen($value);
    $end = '';
    switch ($value[0])
    {
    case 's':
    if ($value[$length - 2] !== '"')
    {
    return false;
    }
    case 'b':
    case 'i':
    case 'd':
    // This looks odd but it is quicker than isset()ing
    $end .= ';';
    case 'a':
    case 'O':
    $end .= '}';
    if ($value[1] !== ':')
    {
    return false;
    }
    switch ($value[2])
    {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    break;
    default:
    return false;
    }
    case 'N':
    $end .= ';';
    if ($value[$length - 1] !== $end[0])
    {
    return false;
    }
    break;
    default:
    return false;
    }
    if (($result = @unserialize($value)) === false)
    {
    $result = null;
    return false;
    }
    return true;
    }
    }

    Works perfectly

  • Rather than rewriting core code, you should make this into a plugin that performs a before action on the unserialize function. You can intercept the string argument, check if it is sterilize, and convert it to the correct value type as needed. This would be the correct Magento 2 way of doing it.

  • Rather than rewriting core code, you should make this into a plugin that performs a before action on the unserialize function. You can intercept the string argument, check if it is sterilize, and convert it to the correct value type as needed. This would be the correct Magento 2 way of doing it.

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

23 hours ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

23 hours ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago