我的 php 代码中出现 fatal error ,想知道如何修复它。
fatal error :PHP fatal error :未捕获错误:调用/home4/metis/public_html/staging/4326/wp-content/plugins/code-snippets/php/中的成员函数 is_empty() snippet-ops.php(446) :eval() 的代码:7
代码:
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_add_to_cart_text', 10, 2 );
function woocommerce_custom_add_to_cart_text( $add_to_cart_text, $product ) {
// Get cart
$cart = WC()->cart;
// If cart is NOT empty
if ( ! $cart->is_empty() ) {. //line 7
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get product id in cart
$_product_id = $cart_item['product_id'];
// Compare
if ( $product->get_id() == $_product_id ) {
// Change text
$add_to_cart_text = 'Already in cart';
break;
}
}
}
return $add_to_cart_text;
}
我想做的是,如果产品在购物车中,“添加到购物车”按钮文本应更改为“已在购物车中”。
最佳答案
问题似乎是 $cart 为 null,也许可以尝试:
if ( !is_null($cart) && !$cart->is_empty() ) {. //line 7
或者检查为什么 WC()->cart;正在返回 NULL
关于php - Uncaught Error : Call to a member function is_empty() on null - add to cart text change - wordpress/woocommerce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64519348/