This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
Single Product Template > Extensions can trigger fatal errors on templates without any post content blocks #10126
Labels
focus: template
Related to API powering block template functionality in the Site Editor
type: bug
The issue/PR concerns a confirmed bug.
type: compatibility
Describe the bug
When plugin developers add a callback function to an action or filter hook within the Single Product template, it is not uncommon for them to assume the
global $product
variable is always available and an instance ofWC_Product
.While this assumption is not ideal (plugin developers should be guarding against fatal errors), this has been a relatively "safe" practice for the classic Product Template, as the core of WooCommerce ensures this variable is properly set via the
woocommerce_content
, which calls thethe_post()
function. Whenthe_post
is called, WooCommerce then puts product data into a global viawc_setup_product_data
.With the blockified version of the Single Product template, though, the presence of the global
$product
as an instance ofWC_Product
is not reliable, as it currently depends on the presence of at least one block that invokes thethe_post()
to work as expected.In other words, by simply removing both the Product Details and the Related Products blocks from the blockified template, the global
$product
variable is not available as an object anymore, resulting in a fatal error for any extensions with callback functions that assume the global$product
is aWC_Product
.To reproduce
Steps to reproduce the behavior:
woocommerce-gutenberg-products-block.php
file), add the following:Edit Site > Templates > Single Product
again.Product Details
and theRelated Products
blocks and save.Expected behavior
The global
$product
variable should be always an instance of theWC_Product
, independent of which blocks are being used within the Single Product template.Additional context
We have a number of folks reporting they are getting fatal errors as a result of this limitation, including conflicts with plugins such as
WC Payments
andWooCommerce Google Analytics Integration
.ref. p1688573619036959-slack-C02UBB1EPEF
The text was updated successfully, but these errors were encountered: