Skip to content

Fatal error on product-compare page when attributes not configured #20229

@Brengineer

Description

@Brengineer

Preconditions

  1. Magento 2.3.x

Steps to reproduce

  1. 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).
  2. Go to the compare page (<site_root>/catalog/product_compare/index)

Expected result

  1. Compare page should load normally, where any unconfigured attributes show "N/A" or "No".

Actual result

  1. "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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: CatalogFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releasegood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions