Fatal error: Call to member function get_id() on null on Page w/Product Blocks
-
A fatal error occurs when any WordPress page that is not a WooCommerce product page contains a
woocommerce/single-productblock (or similar WooCommerce block that triggers product rendering). The error is thrown because several methods insrc/Frontend/Product.phprely onglobal $productwithout first checking whether it is null.Fatal Error
Fatal error: Uncaught Error: Call to a member function get_id() on null in .../wp-content/plugins/precart/src/Frontend/Product.php:15Steps to Reproduce
- Install and activate PreCart alongside WooCommerce.
- Create a standard WordPress page (not a shop or product page).
- Add a WooCommerce
Single Productblock to the page and select any product. - Visit the page on the front end.
- Fatal error is thrown.
Root Cause
The following methods use
global $productand immediately call$product->get_id()with no null check:change_add_to_cart_text()display_pre_order_messgae()display_pre_order_badge()
When WooCommerce renders a
woocommerce/single-productblock on a non-WooCommerce page, its block hydration pipeline calls filters and actions (such aswoocommerce_product_add_to_cart_text) where the$productglobal is null. PreCart’s hooked methods then crash on the firstget_id()call.Suggested Fix
Add a null guard at the top of each affected method before any call to
$product->get_id():if ( ! $product || ! is_a( $product, 'WC_Product' ) ) { return; // or return $text; for change_add_to_cart_text() }Impact
This is a fatal error that takes the entire page down for site visitors, not just a display issue. Any site using WooCommerce product blocks outside of the standard shop/product page context will be affected.
You must be logged in to reply to this topic.