【问题标题】:Apply automatically a coupon based on specific cart items count in Woocommerce根据 Woocommerce 中的特定购物车项目计数自动应用优惠券
【发布时间】:2018-09-16 22:45:51
【问题描述】:

我正在尝试自动触发要在购物车中应用的优惠券,特别是当购物车中有 4 件商品时。

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

我正在使用此答案代码的修改版本:
Add WooCommerce coupon code automatically based on product categories

这是我的代码版本:

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 件商品时,我无法让它自动应用优惠券。这似乎是一件简单的事情,但我被困住了。

任何帮助表示赞赏。

【问题讨论】:

    标签: php wordpress woocommerce cart coupon


    【解决方案1】:

    您的代码中有一些小错误和错误。请尝试以下方法:

    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');
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 谢谢!我现在看到了错误。完美运行。这可能是另一个问题,但是您知道是否可以仅针对此自动应用优惠券隐藏默认的 Woocommerce coupon_applied_successfully 警报?
    猜你喜欢
    • 2019-07-24
    • 2019-08-17
    • 1970-01-01
    • 2021-12-28
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    相关资源
    最近更新 更多