product-jsonld.tpl: "Attempt to read property "name" on null" on every product without a brand (PrestaShop 9)
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 82/100
Research direction
The warning comes from the brand block in templates/_partials/microdata/product-jsonld.tpl, around lines 54-57 and 71-74, where $product_manufacturer is read as an object while the controller can set it to null. Compare with the guard already used in catalog/_partials/product-details.tpl. Done when a product without a brand renders no PHP warning in debug mode and the application/ld+json block stays valid JSON, with the brand falling back to the shop name.
Written by the indexing model from the issue text.
Description
Prerequisites
- I understand and accept the project's code of conduct.
- I have already searched in existing issues and found no previous report of this bug.
Describe the bug and add attachments
Since PrestaShop 9.0, ProductController::assignManufacturer() assigns product_manufacturer as null when the product has no brand, and as a presented array otherwise:
https://github.com/PrestaShop/PrestaShop/blob/9.2.0/controllers/front/ProductController.php#L883-L900
(8.2.x always assigned a Manufacturer object.)
templates/_partials/microdata/product-jsonld.tpl still reads it as an object:
https://github.com/PrestaShop/classic-theme/blob/3.1.2/templates/_partials/microdata/product-jsonld.tpl#L54-L57
{if $product_manufacturer->name OR $shop.name},
...
"name": "{if $product_manufacturer->name}{$product_manufacturer->name|escape:'html':'UTF-8'}{else}{$shop.name}{/if}"
On PHP 8 this raises Warning: Attempt to read property "name" on null twice on every product page without a brand. In debug mode the warnings are printed inside the JSON-LD <script>, which is then no longer valid JSON. The same lines are on develop.
catalog/_partials/product-details.tpl was already adapted ({if !empty($product_manufacturer.id)}); Hummingbird guards it with isset($product_manufacturer).
Steps to reproduce
- PrestaShop 9.0.1 (official Docker image) with demo data and the Classic theme; debug mode on.
- Open the page of a product without a brand, e.g. "Pack Mug + Framed poster" (id 15).
- Look at the page source, in the
application/ld+jsonblock.
Expected behavior
No warning; the brand falls back to the shop name, as on 8.2.x.
Actual Result
"brand": {
"@type": "Brand",
"name": "
Warning: Attempt to read property "name" on null in .../product-jsonld.tpl.php on line 74
PrestaShop"
}
plus a first warning for line 71 (the {if} on line 54 of the template).
Suggested fix: use array access with !empty(), which works for null and for the presented array:
{if !empty($product_manufacturer.name) OR $shop.name},
"brand": {
"@type": "Brand",
"name": "{if !empty($product_manufacturer.name)}{$product_manufacturer.name|escape:'html':'UTF-8'}{else}{$shop.name}{/if}"
}
{/if}
PrestaShop version where the bug happens
9.0.1 (reproduced); 9.2.0 has the same controller code
Theme version where the bug happens
Classic 3.0.2 (reproduced); same template in 3.1.2 and on develop
PHP version(s) where the bug happened
8.4
Reported by PrestaChamps.
- Dominant language
- Smarty
- Stars
- 50
- Forks
- 99
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PrestaShop/classic-theme
-
[BUG] TypeError in updatedDeliveryForm when carrier extra content is not the immediate next siblingPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
PrestaShop/classic-theme#214 ·
All issues in PrestaShop/classic-theme
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
golang-malawi/qatarina#502 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 61/100
Maintainers usually reply within 1 day
-
agent-ready area: ui-react priority: p3 type: refactor
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
schlessera/brain-kit#4726 ·
Maintainers usually reply within 1 day