Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Single Product Block Causing Fatal Error on Product Pages For Logged Out Users When Jetpack woocommerce-analytics Module is Active #9453

Closed
katiebethbrown opened this issue May 12, 2023 · 11 comments · Fixed by #9457 or #9475 · May be fixed by #9458
Assignees
Labels
block: single product Issues related to the single product block. priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. type: bug The issue/PR concerns a confirmed bug.

Comments

@katiebethbrown
Copy link

katiebethbrown commented May 12, 2023

Describe the bug

When using the Single Product Block, single product pages return a fatal error for logged out users when the Jetpack woocommerce-analytics module is active.

To reproduce

Steps to reproduce the behavior:

  1. Enable the WooCommerce Analytics Jetpack module.
  2. Use the single product block on the single product template.
  3. Create a product.
  4. Visit the product page on the front end when logged out.
  5. See fatal error.

Expected behavior

For the single product page to load for logged out users.

Error

[11-May-2023 23:26:28 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get_id()on string in /wordpress/plugins/jetpack/12.2-a.5/modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php:264
Stack trace:
#0 /wordpress/core/6.2/wp-includes/class-wp-hook.php(308): Jetpack_WooCommerce_Analytics_Universal->capture_product_view('')
#1 /wordpress/core/6.2/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)
#2 /wordpress/core/6.2/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#3 /wordpress/plugins/woocommerce/7.7.0/packages/woocommerce-blocks/src/Templates/AbstractTemplateCompatibility.php(196): do_action('woocommerce_aft...')
#4 /wordpress/plugins/woocommerce/7.7.0/packages/woocommerce-blocks/src/Templates/SingleProductTemplateCompatibility.php(106): Automattic\WooCommerce\Blocks\Templates\AbstractTemplateCompatibility->get_hooks_buffer(Array, 'after')
#5 /wordpress/plugins/woocommerce/7.7.0/packages/woocommerce-blocks/src/Templates/SingleProductTemplateCompatibility.php(39): Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility->inject_hook_to_first_and_last_blocks('\n\t\t\t\t<div class...', Array, Array)
#6 /wordpress/plugins/woocommerce/7.7.0/packages/woocommerce-blocks/src/Templates/AbstractTemplateCompatibility.php(71): Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility->inject_hooks('\n\t\t\t\t<div class...', Array)
#7 /wordpress/core/6.2/wp-includes/class-wp-hook.php(310): Automattic\WooCommerce\Blocks\Templates\AbstractTemplateCompatibility->Automattic\WooCommerce\Blocks\Templates\{closure}('\n\t\t\t\t<div class...', Array)
#8 /wordpress/core/6.2/wp-includes/plugin.php(205): WP_Hook->apply_filters('\n\t\t\t\t<div class...', Array)
#9 /wordpress/core/6.2/wp-includes/class-wp-block.php(293): apply_filters('render_block', '\n\t\t\t\t<div class...', Array, Object(WP_Block))
#10 /wordpress/core/6.2/wp-includes/blocks.php(1051): WP_Block->render()
#11 /wordpress/core/6.2/wp-includes/blocks.php(1089): render_block(Array)
#12 /wordpress/core/6.2/wp-includes/block-template.php(240): do_blocks('<!-- wp:templat...')
#13 /wordpress/core/6.2/wp-includes/template-canvas.php(12): get_the_block_template_html()
#14 /wordpress/core/6.2/wp-includes/template-loader.php(106): include('/wordpress/core...')
#15 /wordpress/core/6.2/wp-blog-header.php(19): require_once('/wordpress/core...')
#16 /wordpress/core/6.2/index.php(17): require('/wordpress/core...')
#17 {main}
  thrown in /wordpress/plugins/jetpack/12.2-a.5/modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php on line 264

Environment

WordPress (please complete the following information):

  • WordPress version: 6.2
  • Gutenberg version (if installed): 15.7.1
  • WooCommerce version: 7.7.0
  • WooCommerce Blocks version: 10.2.0
  • Site language: English
  • Any other plugins installed: WordPress.com eCommerce plan managed plugins
  • Jetpack version: 12.2-a.5

Additional context

We're running into this on a Team51 partner site. Per the dev:

The problem is the plugin woo-gutenberg-products-block at woo-gutenberg-products-block/src/BlockTypes/AddToCartForm.php Line 52

The function access global $product

protected function render( $attributes, $content, $block ) {
global $product;

Then overwrites it

/**
* Trigger the single product add to cart action for each product type.
*
* @since 9.7.0
*/
do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );
$product = ob_get_clean();
if ( ! $product ) {
return '';
}

Related issue in the JP repo:
Automattic/jetpack#11301 (comment)

@katiebethbrown katiebethbrown added the type: bug The issue/PR concerns a confirmed bug. label May 12, 2023
@katiebethbrown katiebethbrown changed the title Single Product Block Causing Fatal Error When Jetpack woocommerce-analytics Module is Active Single Product Block Causing Fatal Error on Product Pages For Logged Out Users When Jetpack woocommerce-analytics Module is Active May 12, 2023
@otakupahp
Copy link

I know hot to solve this, so I would like to do it. But I don't know how to :p

@nerrad nerrad added priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. and removed type: community contribution labels May 12, 2023
@nerrad
Copy link
Contributor

nerrad commented May 12, 2023

cc @danielwrobert and @tjcafferkey - this should be prioritized by the team when folks come online next. The code shouldn't be overwriting the global here. It'd be good to review other blocks and ensure there are no other incorrect global overwrites.

@nerrad
Copy link
Contributor

nerrad commented May 12, 2023

Yah there are other places too:

  • Product Image Gallery Block
  • Single Product Block - might be okay but needs a closer look - at a minimum it looks like the code can be cleaned up a bit. This is also might be a place where the Block Context API can be used instead of global manipulation.

@thealexandrelara
Copy link
Contributor

thealexandrelara commented May 12, 2023

  • Single Product Block - might be okay but needs a closer look - at a minimum it looks like the code can be cleaned up a bit. This is also might be a place where the Block Context API can be used instead of global manipulation.

Regarding the Single Product block currently we have to overwrite the global $post variable because some of the inner blocks that are being used, are variations of the Post Excerpt and Post Title blocks that were not consuming the postId from the context. There were two PRs created and merged in Gutenberg that addressed this issue:

As soon as these changes are merged in the core of WP we can get rid of that piece of code, because we can rely on the postId being consumed from the context.

@nefeline
Copy link
Member

nefeline commented May 15, 2023

Hi there!

The Add to Cart Form block and the Product Gallery Block fixes are available over here (with a topic open for discussion):

Regarding the Single Product block, complementing @thealexandrelara 's response:

As he mentioned, we have already upstreamed changes to the core of Gutenberg to prevent the global variable overwrite; the only problem is that those are not yet part of the core of WordPress (WP is currently running GB version 15.1: the fix for the post title comes with GB 15.2 & the fix for the excerpt is on GB version 15.6), so we've decided to keep the "hacky" approach in place until they do.

It is also important to mention that this block (the Single Product block) is experimental, available only on dev builds as it is still not ready for usage by the general public.

@katiebethbrown
Copy link
Author

katiebethbrown commented Jun 28, 2023

It is also important to mention that this block (the Single Product block) is experimental, available only on dev builds as it is still not ready for usage by the general public.

@nefeline Circling back to note that we're showing the option to convert to the blockified single product template in the site editor on WPcom Commerce sites (where I ran into this) with no warning or notice that this shouldn't be used on production sites.

image

Is that expected?

EDIT:
Ah, I realize now that I added the additional blocks plugin to use the mini cart and hadn't realized that this behavior was stemming from it instead of core.

Given the experimental status of this (and potentially all other components in the plugin?) would you suggest we avoid the blocks plugin completely on production Team51 sites?

@nerrad
Copy link
Contributor

nerrad commented Jun 29, 2023

Hi Katie, it's no longer experimental :)

@nefeline
Copy link
Member

nefeline commented Jun 29, 2023

Hi there @katiebethbrown !

Circling back to note that we're showing the option to convert to the blockified single product template in the site editor on WPcom Commerce sites (where I ran into this) with no warning or notice that this shouldn't be used on production sites.

As Darren already highlighted, this is not an experimental feature anymore :)

The Single Product block became available for general usage on June 8th (release 10.4.0).

Given the experimental status of this (and potentially all other components in the plugin?) would you suggest we avoid the blocks plugin completely on production Team51 sites?

It is now available for usage on production sites: looking forward to hearing your thoughts/experience with it!

@katiebethbrown
Copy link
Author

As Darren already highlighted, this is not an experimental feature anymore :)

@nefeline - Ah, I missed this. Thank you!

It is now available for usage on production sites: looking forward to hearing your thoughts/experience with it!

So far all good other than the analytics issues! We ran into something similar to this issue (a fatal for logged out users) when enabling the Google Analytics enhanced ecommerce option, but that was earlier and it seems likely that the fix for this issue also resolved that one.

Thanks again!

@nerrad
Copy link
Contributor

nerrad commented Jun 29, 2023

@katiebethbrown the GA issue came up in another project as well on WPcom (GG) and it is related to customized single product templates where there is no post content block in the template. GA makes an assumption about the $product global always being present and that isn't always the case. This is a separate issue and won't be fixed by what was reported here.

@katiebethbrown
Copy link
Author

Ah, that's really helpful info. Thank you @nerrad!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: single product Issues related to the single product block. priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. type: bug The issue/PR concerns a confirmed bug.
Projects
None yet
5 participants