Php 根据Woocommerce中的特定购物车项目计数自动应用优惠券

Php 根据Woocommerce中的特定购物车项目计数自动应用优惠券,php,wordpress,woocommerce,cart,coupon,Php,Wordpress,Woocommerce,Cart,Coupon,我正在尝试自动触发一个优惠券,以便在购物车中应用,特别是当购物车中有4个项目时 优惠券是在网站“tasterbox”的Woocommerce后端预先创建的 我正在使用此答案代码的修订版本: 以下是我的代码版本: add_action( 'woocommerce_before_calculate_totals', 'wc_auto_add_coupons', 10, 1 ); function wc_auto_add_coupons( $cart_object ) { // Coupo

我正在尝试自动触发一个优惠券,以便在购物车中应用,特别是当购物车中有4个项目时

优惠券是在网站“tasterbox”的Woocommerce后端预先创建的

我正在使用此答案代码的修订版本:

以下是我的代码版本:

add_action( 'woocommerce_before_calculate_totals', 'wc_auto_add_coupons', 10, 1 );
function wc_auto_add_coupons( $cart_object ) {

    // Coupon code
    $coupon = 'tasterbox';

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Initialising variables
    $is_match = false;
    $taster_item_count = 4;

    //  Iterating through each cart item
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // If cart items match 4
        if( $cart->cart_contents_count == $taster_item_count ){
            $is_match = true; // Set to true
            break; // stop the loop
        }
    }

    // If conditions are matched add the coupon discount
    if( $is_match && ! $cart_object->has_discount( $coupon )){
        // Apply the coupon code
        $cart_object->add_discount( $coupon );

        // Optionally display a message 
        wc_add_notice( __('TASTER BOX ADDED'), 'notice');
    } 
    // If conditions are not matched and coupon has been appied
    elseif( ! $has_category && $cart_object->has_discount( $coupon )){
        // Remove the coupon code
        $cart_object->remove_coupon( $coupon );

        // Optionally display a message 
        wc_add_notice( __('SORRY, TASTERBOX NOT VALID'), 'alert');
    }
}
但是,当购物车中有4个项目时,我无法让它自动应用优惠券。这看起来很简单,但我被卡住了


非常感谢您的帮助。

您的代码中有一些小错误。请尝试以下操作:

add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupon_based_on_cart_items_count', 25, 1 );
function auto_add_coupon_based_on_cart_items_count( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Setting and initialising variables
    $coupon = 'tasterbox'; // <===  Coupon code
    $item_count = 4; // <===  <===  Number of items
    $matched    = false;

    if( $cart->cart_contents_count >= $item_count ){
        $matched = true; // Set to true
    }

    // If conditions are matched add coupon is not applied
    if( $matched && ! $cart->has_discount( $coupon )){
        // Apply the coupon code
        $cart->add_discount( $coupon );

        // Optionally display a message
        wc_add_notice( __('TASTER BOX ADDED'), 'notice');
    }
    // If conditions are not matched and coupon has been appied
    elseif( ! $matched && $cart->has_discount( $coupon )){
        // Remove the coupon code
        $cart->remove_coupon( $coupon );

        // Optionally display a message
        wc_add_notice( __('SORRY, TASTERBOX NOT VALID'), 'error');
    }
}
add_动作('woocommerce_前_计算_总数','auto_添加_优惠券_基于_购物车_项目_计数',25,1);
功能自动添加基于购物车的优惠券商品计数($cart){
if(定义了('DOING'uajax'))
返回;
//设置和初始化变量
$优惠券='tasterbox';//有折扣($优惠券)){
//应用优惠券代码
$cart->add_折扣(优惠券);
//可选地显示消息
wc_添加_通知(uuu(“添加了品尝盒”),“通知”);
}
//如果条件不匹配且已申请优惠券
其他(!$matched&&$cart->has_折扣($优惠券)){
//删除优惠券代码
$cart->删除优惠券($优惠券);
//可选地显示消息
wc_添加_通知(_uu('SORRY,TASTERBOX NOT VALID'),'error');
}
}

代码进入活动子主题(或活动主题)的function.php文件。经过测试,效果良好。

谢谢!我现在看到了错误。工作完美。这可能是另一个问题,但您是否知道是否可以仅针对此自动应用优惠券成功隐藏默认的Woocommerce
优惠券
警报?