• Resolved dsawyers

    (@dsawyers)


    A fatal error occurs when any WordPress page that is not a WooCommerce product page contains a woocommerce/single-product block (or similar WooCommerce block that triggers product rendering). The error is thrown because several methods in src/Frontend/Product.php rely on global $product without 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:15

    Steps to Reproduce

    1. Install and activate PreCart alongside WooCommerce.
    2. Create a standard WordPress page (not a shop or product page).
    3. Add a WooCommerce Single Product block to the page and select any product.
    4. Visit the page on the front end.
    5. Fatal error is thrown.

    Root Cause

    The following methods use global $product and 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-product block on a non-WooCommerce page, its block hydration pipeline calls filters and actions (such as woocommerce_product_add_to_cart_text) where the $product global is null. PreCart’s hooked methods then crash on the first get_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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author tasin07

    (@tasin07)

    Thanks for your feedback. We just released an update for this issue. Please update your copy πŸ™‚

    Plugin Author tasin07

    (@tasin07)

    Since we haven’t heard back from you, I’ll go ahead and mark this thread as resolved. If you still need help, feel free to reopen this topic or start a new one. We’re always happy to assist!

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.