Avoid error “call to a member function get_cart()…” in WooCommerce function

只愿长相守 提交于 2021-01-28 07:41:27

问题


Receiving an error on a code that did work previously but now it is stopping me editing a page in wordpress (As when I attempt to edit a page, it states there's been a critical error).

The code below basically changes text for add to cart button depending if the related product is in the cart.

Code below and error exception below that:

function woocommerce_custom_add_to_cart_text($add_to_cart_text, $product ) {
    global $woocommerce;
    // Get cart
    $cart_items = $woocommerce->cart->get_cart();

    foreach ( $cart_items as $cart_item_key => $cart_item ) {
        // Get product id in cart
        $_product_id = $cart_item['product_id'];
 
        if ( $product->get_id() == $_product_id ) {
            $add_to_cart_text = 'Already in cart';
        }
    }

    return $add_to_cart_text;
  
}
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_add_to_cart_text', 10, 2 );

Error exception:

An error of type E_ERROR was caused in line 3 of the file /home4/metisonl/public_html/staging/4326/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code. Error message: Uncaught Error: Call to a member function get_cart() on null in /home4/metisonl/public_html/staging/4326/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:3
Stack trace:
#0 /home4/metisonl/public_html/staging/4326/wp-includes/class-wp-hook.php(287): woocommerce_custom_add_to_cart_text('Add to cart', Object(WC_Product_Simple))
#1 /home4/metisonl/public_html/staging/4326/wp-includes/plugin.php(206): WP_Hook->apply_filters('Add to cart', Array)
#2 /home4/metisonl/public_html/staging/4326/wp-content/plugins/woocommerce/includes/class-wc-product-simple.php(62): apply_filters('woocommerce_pro...', 'Add to cart', Object(WC_Product_Simple))
#3 /home4/metisonl/public_html/staging/4326/wp-content/plugins/woo-gutenberg-products-block/src/BlockTypes/AbstractProductGrid.php(473): WC_Product_Simple->add_to_cart_text()
#4 /home4/metisonl/public_html/staging/4326/wp-content/plugins/woo-gutenberg-products-block/src/BlockTypes/AbstractProductGrid.php(446): Automattic\WooCommerce\Blocks\BlockTypes\Abstra

回答1:


Updated … You need to check that cart object exist and that cart is not empty, so try the following to avoid this kind of error:

add_filter( 'woocommerce_product_add_to_cart_text', 'filter_product_add_to_cart_text', 10, 2 );
function filter_product_add_to_cart_text( $add_to_cart_text, $product ) {
    if ( ! WC()->cart && WC()->cart->is_empty() ) 
        return $add_to_cart_text;

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Get product id in cart
        $_product_id = $cart_item['product_id'];
 
        if ( $product->get_id() == $_product_id ) {
            return __('Already in cart');
        }
    }
    return $add_to_cart_text;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.



来源:https://stackoverflow.com/questions/64425146/avoid-error-call-to-a-member-function-get-cart-in-woocommerce-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!