Skip to content
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

Closed
nefeline opened this issue Jul 6, 2023 · 1 comment · Fixed by #10128
Assignees
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

Comments

@nefeline
Copy link
Contributor

nefeline commented Jul 6, 2023

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 of WC_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 the the_post() function. When the_post is called, WooCommerce then puts product data into a global via wc_setup_product_data.

With the blockified version of the Single Product template, though, the presence of the global $product as an instance of WC_Product is not reliable, as it currently depends on the presence of at least one block that invokes the the_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 a WC_Product.

To reproduce

Steps to reproduce the behavior:

  1. Make sure you have a block theme enabled, such as Twenty-twenty three.
  2. Head over to Edit Site > Templates > Single Product
  3. Make sure you have the blockyified version of this template up and running: if you don't, click on the "Transform into blocks" button and save.
  4. Within any active plugin (e.g. within the woocommerce-gutenberg-products-block.php file), add the following:
add_action( 'woocommerce_single_product_summary', function () {
	global $product;

	$product->get_title();
} );
  1. Access any Single Product on the front-end and ensure everything works as expected.
  2. Now head over to Edit Site > Templates > Single Product again.
  3. Delete/remove both the Product Details and the Related Products blocks and save.
  4. Access any Single Product on the frontend and notice how a Fatal Error is now triggered:
Fatal error: Uncaught Error: Call to a member function get_title() on string

Expected behavior

The global $product variable should be always an instance of the WC_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 and WooCommerce Google Analytics Integration.

ref. p1688573619036959-slack-C02UBB1EPEF

@nefeline nefeline added the type: bug The issue/PR concerns a confirmed bug. label Jul 6, 2023
@nefeline nefeline self-assigned this Jul 6, 2023
@nefeline nefeline changed the title Single Product Compatibility Layer > Extensions can trigger fatal errors on templates without any post content blocks Single Product Template > Extensions can trigger fatal errors on templates without any post content blocks Jul 6, 2023
@nefeline nefeline added focus: template Related to API powering block template functionality in the Site Editor type: compatibility and removed type: compatibility labels Jul 7, 2023
@dinhtungdu
Copy link
Member

FYI, WP 6.4 will also fix this one WordPress/WordPress@5b6555a

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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
Projects
None yet
3 participants