fix fatal error in WooCommerce 6.4.1
-
I’m guessing that this plugin is no longer supported. I’m just sharing this to help anyone who is getting the same error I just started to get:
in the debug log:
[29-Apr-2022 19:37:18 UTC] PHP Fatal error: Uncaught Error: Call to a member function get_id() on null in /home/customer/www/***********.***/public_html/wp-content/plugins/book-previewer-for-woocommerce/inc/functions.php:36 Stack trace: #0 /home/customer/www/***********.***/public_html/wp-includes/class-wp-hook.php(309): bpfw_change_template_product_image('single-product/...', 'single-product/...') #1 /home/customer/www/***********.***/public_html/wp-includes/plugin.php(189): WP_Hook->apply_filters('single-product/...', Array) #2 /home/customer/www/***********.***/public_html/wp-content/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-system-status-v2-controller.php(1051): apply_filters('wc_get_template', 'single-product/...', 'single-product/...', Array, 'framework/admin...', '/home/customer/...') #3 /home/customer/www/***********.***/public_html/wp-content/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-system-status-v2-controller.php(594): WC_REST_System_Status_V2_Controller->get_theme_info() #4 in /home/customer/www/***********.***/public_html/wp-content/plugins/book-previewer-for-woocommerce/inc/functions.php on line 36
And this is showing up as a fatal error on this page:
wp-admin/admin.php?page=wc-status
which is the WooCommerce > Status page on WooCommerce version 6.4.1 and it triggers the “critical error” email.The code in question is not strictly from this plugin but from a fix to this plugin that you can find here: https://wordpress.org/support/topic/disable-for-other-products/#post-14212255 (read to the end of that thread for more solution info)
There’s some great code from @talus that you can find in there that contains this bit:
if ($template_name === 'single-product/product-image.php') { global $product; if( 'yes' == get_post_meta( $product->get_id(), '_enable_book_preview', true ) ){ return BPFW()->locate_template('product-image.php'); } } return $template;
It’s the “get_id()” call that is breaking when there is no product (i.e. this gets executed now in a context where it didn’t get executed before.) So now we need to add a check to see if we have a product before we do the “get_id()”.
This is the code that fixed it for me (in /wp-content/plugins/book-previewer-for-woocommerce/inc/functions.php):
if ($template_name === 'single-product/product-image.php') { global $product; if(!empty($product)){ if( 'yes' == get_post_meta( $product->get_id(), '_enable_book_preview', true ) ){ return BPFW()->locate_template('product-image.php'); } } /** end added if **/ } return $template;
- The topic ‘fix fatal error in WooCommerce 6.4.1’ is closed to new replies.