• Resolved joycegrace

    (@joycegrace)


    Hello,
    When I try to add a featured product block, I get a spinning wheel for a few seconds and then immediately, this message:

    The following error was returned
    <p>There has been a critical error on this website.</p><p>Learn more about troubleshooting WordPress.</p>

    See screenshot: https://cloudup.com/c3CJBfTpZ5S

    I deactivated all plugins except woocommerce and woocommerce blocks, same issue hapens.

    Works with Twenty Twenty One theme. What does my theme need to support this feature? Using Genesis as the parent with a child theme.

    Debug log has this as the latest error when I try to do this (not sure if it’s related):

    [06-Jun-2021 00:19:49 UTC] PHP Fatal error:  Uncaught Error: Call to a member function is_type() on null in /nas/content/live/*installname*/wp-content/themes/*child-theme-name*/functions.php:421
    Stack trace:
    #0 /nas/content/live/*installname*/wp-includes/class-wp-hook.php(294): {closure}('Add to cart')
    #1 /nas/content/live/*installname*/wp-includes/plugin.php(212): WP_Hook->apply_filters('Add to cart', Array)
    #2 /nas/content/live/*installname*/wp-content/plugins/woocommerce/includes/class-wc-product-simple.php(62): apply_filters('woocommerce_pro...', 'Add to cart', Object(WC_Product_Simple))
    #3 /nas/content/live/*installname*/wp-content/plugins/woo-gutenberg-products-block/src/StoreApi/Schemas/ProductSchema.php(460): WC_Product_Simple->add_to_cart_text()
    #4 /nas/content/live/*installname*/wp-content/plugins/woo-gutenberg-products-block/src/StoreApi/Routes/Products.php(56): Automattic\WooCommerce\Blocks\StoreApi\Schemas\ProductSchema->get_item_response(Object(WC_Product_Simple))
    #5 /nas/content/live/*installname*/wp-content/plugins/woo-gutenberg-products-block/src/StoreApi in /nas/content/live/*installname*/wp-content/themes/*child-theme-name*/functions.php on line 421
    [06-Jun-2021 00:20:16 UTC] auditor:event=switch_theme {"new_name":"Twenty Twenty-One","blog_id":1,"event":"switch_theme","current_user_id":2,"remote_addr":"*000.000.000.00*"}
    

    This is what is on lines 418 – 425 in functions.php (per the line 421 referenced above):

    /*Change Select Options Text */
    add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
    	global $product;
    	if ( $product->is_type( 'variable' ) ) {
    		$text = $product->is_purchasable() ? __( 'Options', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
    	}
    	return $text;
    }, 10 ); 
    

    WP Core 5.7.2
    PHP 7.4
    All themes and plugins are up to date
    On a WP Engine dev environment

    Thank you

    • This topic was modified 3 years, 5 months ago by joycegrace. Reason: wrong error
    • This topic was modified 3 years, 5 months ago by joycegrace.
    • This topic was modified 3 years, 5 months ago by joycegrace.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Thu P. a11n

    (@thup90)

    Hi there,

    Works with Twenty Twenty One theme. What does my theme need to support this feature? Using Genesis as the parent with a child theme.

    Looking at the log, it looks like your child theme generated a fatal error with the product block. I’d recommend reviewing your code or remove completely the code that causes the error.

    If you need further help with this, I’d recommend reaching out to StudioPress for troubleshooting:

    https://www.studiopress.com/contact/

    Feel free to get back to us in case you have any additional questions!

    I concur with @thup90 that this is an issue caused by your child theme’s functions.php file as noted in path of the fatal error in your logs. The problem is that the $product global referenced in the filter callback is not necessarily always populated in all contexts where the woocommerce_product_add_to_cart_text filter is called.

    Instead of using the global, a better solution would be to use the product instance provided to the filter callback – something like this “should” stop the fatal error from happening (untested and use at your own risk):

    
    /*Change Select Options Text */
    add_filter( 'woocommerce_product_add_to_cart_text', function( $text, $product ) {
    	if ( ! $product instanceof WC_Product ) { 
                return $text;
            }
    	if ( $product->is_type( 'variable' ) ) {
    		$text = $product->is_purchasable() ? __( 'Options', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
    	}
    	return $text;
    }, 10, 2 );
    
    Plugin Support Dani F. a11n

    (@danielinhou)

    Hi there!

    Just posting here to say that you could also try to change that text using the Say What plugin too. That would allow you to delete the code that is causing the conflict.

    This tutorial might be useful for that

    I think that the issue has been addressed so I am marking this thread as resolved but feel free to answer back here if you have any further doubts and I will reopen it.

    Cheers!

    Thread Starter joycegrace

    (@joycegrace)

    Thank you all very much! I’ll try these out and report back here

    • This reply was modified 3 years, 5 months ago by joycegrace.
    Thread Starter joycegrace

    (@joycegrace)

    @nerrad your code seemed to work and now the blocks work – thank you all very, very much!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Featured block says critical error’ is closed to new replies.