When I was updating my magento 2.3 to 2.4.x, PHP 8.1 is required, During the upgrade, I encountered the following coding errors which was either deprecated or hit critical error and need to update to.
Hence, I am sharing the following errors and the workaround.
1. Error in loading Magento Catalog Model Product
Error encounter:
[2023-03-06T06:20:39.792966+00:00] main.CRITICAL: Error: Call to undefined method Magento\Catalog\Model\Product\Type\Simple\Interceptor::
() in /var/www/vhosts/xxx/vendor/magento/module-configurable-product/Helper/Product/Options/Loader.php:52
Stack trace:
Workaround:
Change from
$objectManager->get('\Magento\Catalog\Model\Product')->load($productId)
to
$objectManager->create('\Magento\Catalog\Model\Product')->load($productId)
2. PHP Deprecated: trim(): Passing null to parameter #1
Error encounter:
PHP Deprecated: trim(): Passing null to parameter #1
Workaround:
Change from
return trim($x);
to
return trim((string)$x);
3. explode(): Argument ($string) must be of type string
Error encounter:
#100 {main} {"exception":"[object] (TypeError(code: 0): explode(): Argument #2 ($string) must be of type string, array given at /././.....
Workaround:
Change from
$value = explode(',', $requestValue);
to
if(is_array($requestValue)) {
$value = $requestValue;
}else{
$value = explode(',', $requestValue);
}
4. strftime is deprecated
Error encounter:
This function has been DEPRECATED as of PHP 8.1.0. Relying on this function is highly discouraged.
Workaround:
Change from
$now = strftime('%Y-%m-%d', time() );
to
$now = date('Y-m-d', time() );