0

I get an error on Magento 2.3.5-p1 when trying to add products to an existing category. The products won't save and I get this message:

Argument 1 passed to Magento\Catalog\Model\Category\FileInfo::removeStorePath() must be of the type string, array given, called in /cache/vendor/magento/module-catalog/Model/Category/FileInfo.php on line 167

If I navigate to the product page to add to a category i get this error:

Unable to unserialize value. Error: Syntax error

and this in the log:

[2020-09-18 19:44:02] main.CRITICAL: Unable to unserialize value. Error: Syntax error {"exception":"[object] (InvalidArgumentException(code: 0): Unable to unserialize value. Error: Syntax error at /cache/vendor/magento/framework/Serialize/Serializer/Json.php:39)"} []

How can I fix this?

4
  • Please share your code as well Sep 19, 2020 at 2:11
  • 1
    Check you've updated your 3rd party modules. They could be using the php seralize methods instead of using jsons and json encode. That was a change from 2.3.4 to 2.3.5-*
    – Mark Rees
    Sep 19, 2020 at 9:58
  • I just upgrade to 2.4 and disable all third party extension and the issue is still there. My code is the basic magento codes for these pages.
    – Emili
    Sep 21, 2020 at 21:19
  • Try to go to vendor/magento/module-catalog/Model/Category/FileInfo.php and debug line 167, "$filePath = $this->removeStorePath($fileName);", check what you're sending in that $fileName, might help getting an hint on what's going on
    – Rui Silva
    Sep 22, 2020 at 11:45

3 Answers 3

0
  1. This issue can be caused because of improper file permission ( generate ).

  2. If you are using redis try to flush it - redis-cli flushall

  3. Problem with your cache - Flush it and then try

  4. the last one make changes in Json.php ( Please first try the three above )

// Change unserialize function with this one

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;
}

// Add this on before it

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;
}

Find More info in this page Magento 2.2: Unable to unserialize value?

0

I think the below code will help you to fix the serialization error.

namespace M2\Mage24Fix\Framework\Serialize\Serializer;

/**
 * Class Title
 * 
 * Fix fatal with serialize json for 2.4.0 version - https://magento.stackexchange.com/questions/286634/magento-2-3-xx-unable-to-serialize-value-error-malformed-utf-8-characters-pos
 */
class Json extends \Magento\Framework\Serialize\Serializer\Json
{
    /**
     * @inheritDoc
     * @since 101.0.0   
     */
    public function serialize($data)
    {
        try{
            $result = json_encode($data);
            if (false === $result) {
                throw new \InvalidArgumentException("Unable to serialize value. Error: " . json_last_error_msg());
            }
        } catch (\Exception $e){
            $result = json_encode( $this->utf8ize( $data ) );
        }
        return $result;
    }

    /**
     * @inheritDoc
     * @since 101.0.0
     */
    public function utf8ize( $mixed ) {
        if (is_array($mixed)) foreach ($mixed as $key => $value) $mixed[$key] = $this->utf8ize($value);
        elseif (is_string($mixed)) return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
        return $mixed;
    }
}

Please override this file in app/code/M2 custom module. PHP bin/magento setup:upgrade PHP bin/magento cache:flush

Please let me know if this will not work. Thahks.

0

Emili,

I checked the Magento 2.3.5-p1 code base,

The exception is thrown by an attribute that Backend model instance of Image.

Backend model

=>

  • Please review your EAV attributes (eav_attribute) which have backend model like '%image%',
  • Check the before load, before save, after load, after save, makes sure the attribute's value afterload isn't instance of an array.

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.