微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据WooCommerce中的客户送货区域应用特定的优惠券

如何解决根据WooCommerce中的客户送货区域应用特定的优惠券

我有两个优惠券和多个运输区域

  • (运输区域A,运输区域B,运输区域C)
  • (优惠券1)和(优惠券2)。 我想要的是,如果用户选择特定的运输区域,可以说(运输区域C) 我想为(运输区域C)申请(优惠券2)。 和(优惠券1)将适用于所有其他运输区域。

这是我到目前为止尝试过的

<?PHP
add_action('woocommerce_before_cart','add_coupon_notice');
add_action('woocommerce_before_checkout_form','add_coupon_notice');
function add_coupon_notice()
{
    if (is_cart() || is_checkout())
    {
        global $woocommerce;
        $cart_total = WC()
            ->cart->total;
        $minimum_amount = 100;
        $currency_code = get_woocommerce_currency();
        $states = array(
            'shipping zone A','shipping zone B','shipping zone C'
        );
        wc_clear_notices();
        if ($cart_total < $minimum_amount && in_array(WC()
            ->customer
            ->get_shipping_state(),$states))
        {
            WC()
                ->cart
                ->apply_coupon('COUPON 1');
            wc_print_notice('You just got 15% off your order!','notice');
        }
        else
        {
            WC()
                ->cart
                ->apply_coupon('COUPON 2');
            wc_print_notice('You just got 15% off your order!','notice');
        }
        wc_clear_notices();
    }
}

解决方法

我设法用下面的代码解决了这个问题。

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
    function woocommerce_custom_surcharge() {
        global $woocommerce;
        $state = $woocommerce->customer->get_shipping_state();       
        if ( $state == 'shipping zone 1' ) {
            WC()->cart->apply_coupon( 'COUPON 2' );
            WC()->cart->remove_coupon( 'COUPON 1' );   
        } else {
             WC()->cart->apply_coupon( 'COUPON 1' );
             WC()->cart->remove_coupon( 'COUPON 2' );
        } 
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。