避免出现错误“调用成员函数get_cart()…".在WooCommerce功能中 [英] Avoid error "call to a member function get_cart()…" in WooCommerce function
问题描述
在以前可以正常工作的代码上收到错误,但现在它使我无法在wordpress中编辑页面(因为当我尝试编辑页面时,它指出存在严重错误).
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 );
错误异常:
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
推荐答案
已更新…您需要检查购物车对象是否存在以及购物车是否为空,因此请尝试以下操作以避免这种情况错误:
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;
}
代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
这篇关于避免出现错误“调用成员函数get_cart()…".在WooCommerce功能中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!