php 根据WooCommerce中的客户运输区域应用特定优惠券

z9smfwbn  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(119)

我有两张优惠券和多个送货区

  • (装运区A、装运区B、装运区C)
  • (优惠券1)和(优惠券2)。我想要的是,如果用户选择了特定的装运区,比如说(装运区C),我想要具体地申请(优惠券2)用于(装运区C)。并且(优惠券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();
    }
}
af7jpaap

af7jpaap1#

我设法解决它与下面的代码.

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

相关问题