Error adding standard WooCommerce product widget to blog post

Viewed 45

I am using a code that changes the text and style of the Add to Cart button for a product added to the cart. (All CSS styles for my theme)

/* Change the text of the add to cart button for the archive and categories */
add_filter( 'woocommerce_product_add_to_cart_text', 'new_products_button_text', 20, 2 );
 
function new_products_button_text( $text, $product ) {
 
    if( 
       $product->is_type( 'simple' )
       && $product->is_purchasable()
       && $product->is_in_stock()
       && WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) )
    ) { 
        $text = 'Product in Cart'; 
    }
 
    return $text;
 
}

/* Change the add to cart button text for the product page */
add_filter( 'woocommerce_product_single_add_to_cart_text', 'new_single_product_button_text' );
 
function new_single_product_button_text( $text ) {
 
    if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( get_the_ID() ) ) ) {
        $text = 'Product in Cart';
    }
 
    return $text;
 
}

add_action( 'wp_footer', 'action_wp_footer' );
function action_wp_footer() {
    ?>
    <script>
        jQuery(document).ready(function($) {
            var selector = '.add_to_cart_text:contains("Product in Cart")';         
            
            // Selector contains specific text
            if ( $( selector ).length > 0 ) {
                $( selector ).addClass( 'product-is-added' );
            } else {
                $( selector ).removeClass( 'product-is-added' );            
            }
            
        });
    </script>
    <?php
}

add_action( 'wp_footer', 'ajax_button_text_js_script' );
function ajax_button_text_js_script() {
    $text = __('Product in Cart', 'woocommerce');
    ?>
    <script>
        jQuery(function($) {
            var text = '<?php echo $text; ?>',      $this;

            $(document.body).on('click', '.ajax_add_to_cart', function(event){
                $this = $(this); // Get button jQuery Object and set it in a variable
            });

            $(document.body).on('added_to_cart', function(event,b,data){
                var buttonText = '<span class="add_to_cart_text product-is-added">'+text+'</span><i class="cart-icon pe-7s-cart"></i>';

                // Change inner button html (with text) and Change "data-tip" attribute value
                $this.html(buttonText).attr('data-tip',text);
            });
        });
    </script>
    <?php
}

The code works correctly, but there was a problem with the blogs. In blog posts, I add a standard WooCommerce product widget. I use WPBakery Page Builder, the visual editor.

When publishing or saving a post with a standard WooCommerce widget, I get a critical error:

Fatal error: Uncaught Error: Call to a member function find_product_in_cart() on null in /public_html/wp-content/themes/functions.php:703 Stack trace: #0 /public_html/wp-includes/class-wp-hook.php(292): new_products_button_text('\xD0\x92 \xD0\xBA\xD0\xBE\xD1\x80\xD0\xB7\xD0\xB8\xD0\xBD...', Object(WC_Product_Simple)) #1 /public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters('\xD0\x92 \xD0\xBA\xD0\xBE\xD1\x80\xD0\xB7\xD0\xB8\xD0\xBD...', Array) #2 /public_html/wp-content/plugins/woocommerce/includes/class-wc-product-simple.php(62): apply_filters('woocommerce_pro...', '\xD0\x92 \xD0\xBA\xD0\xBE\xD1\x80\xD0\xB7\xD0\xB8\xD0\xBD...', Object(WC_Product_Simple)) #3 /public_html/wp-content/themes/cores/nasa-woo-functions.php(393): WC_Product_Simple->add_to_cart_text() #4 /public_html/wp-content/themes/cores/nasa-woo- in /public_html/wp-content/themes/functions.php on line 703

Without a product widget, the post is published without any problems.

Line 703 is part of the above code:

&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) )

As I understand it, the cart is not initialized in the admin panel, since it is not needed here.

How can this be fixed?

I will be glad for your help!

1 Answers

In exceptional cases or due to the combination of plugins/themes you can indeed run into error messages.

A solution for this could be to add multiple checks before executing the next piece of code, in this way you can prevent error messages.

For example, you can replace your existing new_products_button_text callback function with:

function new_products_button_text( $text, $product ) {
    if ( is_admin() ) return $text;

    if ( $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() ) {
        // WC Cart
        if ( WC()->cart ) {
            // Get cart
            $cart = WC()->cart;
            
            // If cart is NOT empty
            if ( ! $cart->is_empty() ) {
                // Find product in cart
                if ( $cart->find_product_in_cart( $cart->generate_cart_id( $product->get_id() ) ) ) {
                    $text = 'Product in Cart';
                }
            }
        }
    }

    return $text;
}

This can be written more compactly, but by spreading the conditions over multiple if statements, you can quickly determine where things are going wrong, if you still receive an error message.

Related