【问题标题】:Auto apply coupon based on WooCommerce cart subtotal in which certain products are excluded根据排除某些产品的 WooCommerce 购物车小计自动应用优惠券
【发布时间】:2021-12-28 00:31:09
【问题描述】:

当客户的购物车中有 100 美元或更多时,我正在使用以下代码自动应用优惠券。

add_action( 'woocommerce_checkout_before_order_review' , 'add_coupon_notice' );
function add_coupon_notice() { 
    $cart_total = WC()->cart->get_subtotal();
    $minimum_amount = 100;

    $currency_code = get_woocommerce_currency();
    wc_clear_notices();

    if ( $cart_total < $minimum_amount ) {
        WC()->cart->remove_coupon( '20OFF100' );
        wc_print_notice( "Get 20% off if you spend more than $$minimum_amount", 'notice' );
    } else {
        WC()->cart->apply_coupon( '20OFF100' );
        wc_print_notice( '20% off $100 or more - Discount Applied!', 'notice' );
    }

    wc_clear_notices();
}

但是,我想从最低 100 美元中排除特定产品。

特定产品正在打折,我在优惠券管理屏幕中检查了“排除销售项目”,但下面的代码忽略了这一点。

为什么“排除销售项目”不起作用,和/或我该如何解决?

【问题讨论】:

    标签: wordpress woocommerce product cart coupon


    【解决方案1】:

    您的代码中最大的误解是,即使您在优惠券管理屏幕中选中了“排除销售商品”,但 WC()-&gt;cart-&gt;get_subtotal() 的使用并未考虑到这一点。

    解决办法:

    • 您可以使用woocommerce_before_calculate_totals 挂钩
    • 为避免出现问题,请使用不含大写字母的优惠券代码
    • 在浏览购物车内容时,我们会跟踪总数,排除的产品除外
    • 我们将根据当前金额阈值以及是否已使用优惠券代码,显示通知

    所以你得到:

    function action_woocommerce_before_calculate_totals( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
    
        // Coupon code
        $coupon_code = 'coupon1';
        
        // Total amount threshold
        $amount_threshold = 100;
        
        // Targeted product IDs, several can be entered, separated by a comma
        $targeted_product_ids = array( 30, 813 );
    
        // Initializing variables
        $total_amount = 0;
        $applied_coupons = $cart->get_applied_coupons();
    
        // Loop through cart contents
        foreach( $cart->get_cart_contents() as $cart_item ) {       
            // Excluding targeted product IDs
            if ( ! in_array( $cart_item['data']->get_id(), $targeted_product_ids ) ) {          
                // Get the cart total amount
                $total_amount += $cart_item['line_total'] + $cart_item['line_tax'];
            }
        }
    
        // Applying coupon
        if ( ! in_array( $coupon_code, $applied_coupons ) && $total_amount >= $amount_threshold ) {
            $cart->apply_coupon( $coupon_code );
            wc_clear_notices();
            wc_add_notice( __( 'Succeeded', 'woocommerce' ), 'notice' );        
        }
        // Buy more
        elseif ( ! in_array( $coupon_code, $applied_coupons ) && $total_amount < $amount_threshold ) {
            wc_clear_notices();
            wc_add_notice( __( 'Buy more', 'woocommerce' ), 'notice' ); 
        }
        // Removing coupon
        elseif ( in_array( $coupon_code, $applied_coupons ) && $total_amount < $amount_threshold ) {
            $cart->remove_coupon( $coupon_code );
            wc_clear_notices();
            wc_add_notice( __( 'Removed', 'woocommerce' ), 'notice' );  
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
    

    【讨论】:

      最近更新 更多