Prevent fatal error when product template variable is a Product object#181
Prevent fatal error when product template variable is a Product object#181boo-code wants to merge 1 commit into
Conversation
HookDisplayFooterProduct::run() passes the 'product' Smarty variable straight to ProductWrapper::prepareItemFromProduct(), which accesses it with array keys. The variable is normally a ProductLazyArray (array-accessible), but some themes or modules assign a raw Product object, causing a fatal error "Cannot use object of type Product as array" on the product page. Guard against a non-array-accessible product and skip tracking in that case instead of breaking the whole product page.
|
Hello @boo-code! This is your first pull request on ps_googleanalytics repository of the PrestaShop project. Thank you, and welcome to this Open Source community! |
kpodemski
left a comment
There was a problem hiding this comment.
To me the change is totally fine, and I'm always recommending to check whether the input data is what we expect.
|
Thanks @Hlavtox, that is a fair principle and I do not want this to become a pattern of guarding against every misbehaving module either. Let me explain why I think this specific case is worth it, and offer a middle ground. The distinction I would draw: The failure modes are also very asymmetric. Without the guard, a third party assigning a raw It is also deliberately minimal and does not touch the normal path: On your underlying concern, that we should not silently mask a bad module: I agree, and I am happy to add a debug-level log when the guard trips, so the misconfiguration stays visible and diagnosable instead of being swallowed. If you would prefer that, I will push it; otherwise I am glad to leave the guard as it is. Either way, thanks for the careful read. |
|
Indeed, I also think it's better to avoid the page crashing. |
HookDisplayFooterProduct::run()reads theproductSmarty variable and passes it straight toProductWrapper::prepareItemFromProduct(), which consumes it with array keys ($product['id_product'],$product['name'], …). That variable is normally aProductLazyArray(array-accessible), but some themes/modules assign a rawProductobject to it, producing a fatal errorCannot use object of type Product as arrayon the product page (reported on 5.0.3 / PS 8.2.4). This PR guards the hook: if the product is not array-accessible, it skips tracking instead of breaking the page.ProductLazyArrayimplementsArrayAccess, so the normal flow is unaffected.productSmarty variable is a rawProductobject, before this PR the page fatals withCannot use object of type Product as array(ProductWrapper.phpline ~136). Deterministic check:prepareItemFromProduct(new Product(1))throws that error; with the guard,HookDisplayFooterProduct::run()returns without a fatal. With the standardProductLazyArray(which implementsArrayAccess), tracking continues to work as before.