【发布时间】:2020-08-05 12:39:48
【问题描述】:
我想隐藏购物车页面上所有产品的优惠券字段,具有特定产品类别的产品除外。我创建了以下有效的代码,但每隔一段时间,似乎与时间无关,我会收到一条错误消息。它不会阻止代码工作,也不会导致任何问题。但是,我似乎无法找到收到错误消息的原因或解决方法。
add_filter( 'woocommerce_coupons_enabled', 'wdc_hide_coupon_field_dinner_dances' );
function wdc_hide_coupon_field_dinner_dances($enabled){
$wdc_category = 'discount';
$has_cat = true;
foreach ( WC()->cart->get_cart() as $cart_item_key =>$cart_item ) {
$wdc_product = $cart_item['data'];
$product_id = method_exists( $wdc_product, 'get_id' ) ? $wdc_product->get_id() : $wdc_product->id;
if ( has_term( $wdc_category, 'product_cat', $product_id ) ) $has_cat = false;
}
if ( $has_cat && is_cart() ) {
$enabled = false;
}
return $enabled;
}
我收到此错误消息
错误详情
==============
在文件的第 16 行中导致了 E_ERROR 类型的错误 /home/westviewdance/public_html/wp-content/plugins/WdcFreeTicketCoupon-for-Woocommerce/WdcFreeTicketCoupon 对于 Woocommerce.php。
错误消息:未捕获的错误:在 null 上调用成员函数 get_cart() /home/westviewdance/public_html/wp-content/plugins/WdcFreeTicketCoupon-for-Woocommerce/WdcFreeTicketCoupon
对于 Woocommerce.php:16 堆栈跟踪:
#0 /home/westviewdance/public_html/wp-includes/class-wp-hook.php(287): wdc_hide_coupon_field_dinner_dances(true)
#1 /home/westviewdance/public_html/wp-includes/plugin.php(206): WP_Hook->apply_filters(true, Array)
#2 /home/westviewdance/public_html/wp-content/plugins/woocommerce/includes/wc-coupon-functions.php(69): apply_filters('woocommerce_cou...', true)
#3 /home/westviewdance/public_html/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/Assets.php(157): wc_coupons_enabled()
#4 /home/westviewdance/public_html/wp-includes/class-wp-hook.php(287): Automattic\WooCommerce\Blocks\Assets::get_wc_block_data(Array)
#5 /home/westviewdance/public_html/wp-includes/plugin.php(206): WP_Hook->apply_filters(Array, Array)
#6 /home/westviewdance/public_html/wp-content/plugins/woocommerce/packages/woocommerce-
【问题讨论】:
标签: php woocommerce cart coupon taxonomy-terms