-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Description
Preconditions
- Magento 2.3.x
Steps to reproduce
- Add products to compare where at least one product has no value set for at least one of its comparable attributes (e.g. "Description" is empty).
- Go to the compare page (<site_root>/catalog/product_compare/index)
Expected result
- Compare page should load normally, where any unconfigured attributes show "N/A" or "No".
Actual result
- "Fatal error: Uncaught TypeError: preg_match() expects parameter 2 to be string, object given in [site_root]/vendor/magento/module-catalog/Helper/Output.php on line 229"
The error occurs in the isDirectivesExists()
method, which expects a string param. This method is called by productAttribute()
, which passes $attributeHtml
, which it receives as a param that is also expected to be a string.
The issue is that in app/code/Magento/Catalog/view/frontend/templates/product/compare/list.phtml:119
, where productAttribute()
is called, the 2nd param passed is a value from \Magento\Catalog\Block\Product\Compare\ListCompare::getProductAttributeValue
, which returns a \Magento\Framework\Phrase
object when the value of an attribute is undefined. Therefore, an error is thrown when this object is passed through to preg_match()
.
The issue was triggered in commit #bdcfbc8, where a call to isDirectivesExists()
is added to line 167 of app/code/Magento/Catalog/Helper/Output.php
. However, this is reasonable, since a string is expected. Therefore I recommend simply modifying line 119 of list.phtml
to cast the result of getProductAttributeValue()
as a string, which would be appropriate, since that's what productAttribute()
is expecting.