• Resolved hejhog

    (@hejhog)


    On upgrading to Woocommerce 11 one of my snippets, as below, produced a critical error when viewing a product.

    add_filter('woocommerce_product_add_to_cart_text','bbloomer_archive_custom_cart_button_text');

    function bbloomer_archive_custom_cart_button_text( $text ) {

    global $product;
    if ( $product && !$product->is_in_stock() ) {
    // code etc
    }
    }

    The reason being is that the the first instance of $product is now a string instead of an object. Is this a bug?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support shahzeen(woo-hc)

    (@shahzeenfarooq)

    Hi @hejhog!

    Thank you for sharing the details and the snippet. I understand the issue: after updating to WooCommerce 11, the snippet causes a critical error because $product is being received as a string rather than a WC_Product object.

    This is not necessarily a WooCommerce bug. The snippet relies on the global $product variable being available as a product object when the woocommerce_product_add_to_cart_text filter runs. Custom snippets that rely on globals can be affected when the surrounding WooCommerce code or execution context changes.

    If you need more in-depth support or want to consider professional assistance for customization, I can recommend WooExperts and Codeable.io as options for getting professional help. Alternatively, you can also ask your development questions in the  WooCommerce Community Slack as custom code falls outside our usual scope of support.

    Thread Starter hejhog

    (@hejhog)

    Thanks. Just confirming this is not a bug. An easy work around as follows:

    add_filter('woocommerce_product_add_to_cart_text','bbloomer_archive_custom_cart_button_text');

    function bbloomer_archive_custom_cart_button_text( $text ) {
    global $product;

    if (gettype($product) == "object" && is_subclass_of($product, 'WC_Product')) {
    if ( !$product->is_in_stock() ) {
    //code etc
    }
    }
    }
Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.