【发布时间】:2019-08-17 04:57:15
【问题描述】:
如果购物车至少有 2 件商品,我想将优惠券代码应用于购物车。如果没有,则优惠券将不适用并显示更改消息,如果已申请,则将显示成功消息这是我的代码我尝试过的代码不能像我想要的那样工作
add_action( 'woocommerce_before_calculate_totals','conditionally_auto_add_coupon', 30, 1 );
function conditionally_auto_add_coupon( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') ) return; // Exit
// HERE set the coupon code (in lowercase)
$coupon_code = 'mycode';
$total_item = 0;
if (WC()->cart->has_discount('mycode')) {
foreach( $cart->get_cart() as $cart_item ){
$total_item++;
}
if($total_item < 2){
$cart->remove_coupon( $coupon_code );
wc_add_notice( __('you have only 1 item in cart'), 'alert');
}
else{
$cart->add_discount( $coupon_code );
wc_add_notice( __('coupon added'), 'notice');
}
}
}
欢迎任何帮助。
【问题讨论】:
标签: php wordpress woocommerce cart coupon