当折扣小计低于 75 时,隐藏特定应用优惠券的免费送货

Hide Free shipping for specific applied coupon when discounted subtotal is below 75

我正在尝试创建一种非常特殊的优惠券代码。

现在该网站已经提供免费送货服务,但如果订单低于 75 英镑,我希望禁用这些服务,仅当应用此代码时

使用 Whosebug 上的其他问题,我成功地创建了代码,但它被应用于每一对夫妇。

如何将此代码仅应用于 "allthings30" 优惠券。任何帮助都非常适用。

add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 33, 38 );
function coupons_removes_free_shipping( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    $min_subtotal      = 75; // Minimal subtotal allowing free shipping
    $coupon_code    = 'allthings30'; // The required coupon code

    // Get needed cart subtotals
    $subtotal_excl_tax = WC()->cart->get_subtotal();
    $subtotal_incl_tax = $subtotal_excl_tax + WC()->cart->get_subtotal_tax();
    $discount_excl_tax = WC()->cart->get_discount_total();
    $discount_incl_tax = $discount_total + WC()->cart->get_discount_tax();

    // Calculating the discounted subtotal including taxes
    $discounted_subtotal_incl_taxes = $subtotal_incl_tax - $discount_incl_tax;

    $applied_coupons = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );

    if( sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
        foreach ( $rates as $rate_key => $rate ){
            // Targeting "Free shipping"
            if( 'free_shipping' === $rate->method_id  ){
                unset($rates[$rate_key]);
            }
        }
    }
    return $rates;
}

你离...很近了:

add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 33, 38 );
function coupons_removes_free_shipping( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    $min_subtotal   = 75; // Minimal subtotal allowing free shipping
    $coupon_code    = 'summer'; // The required coupon code

    // Get cart subtotals and applied coupons
    $cart              = WC()->cart;
    $subtotal_excl_tax = $cart->get_subtotal();
    $subtotal_incl_tax = $subtotal_excl_tax + $cart->get_subtotal_tax();
    $discount_excl_tax = $cart->get_discount_total();
    $discount_incl_tax = $discount_excl_tax + $cart->get_discount_tax();
    $applied_coupons   = $cart->get_applied_coupons(); // Get applied coupons array

    // Calculating the discounted subtotal including taxes
    $disc_subtotal_incl_tax = $subtotal_incl_tax - $discount_incl_tax;

    if( in_array( strtolower($coupon_code), $applied_coupons ) && $disc_subtotal_incl_tax < $min_subtotal ){
        foreach ( $rates as $rate_key => $rate ){
            // Targeting "Free shipping"
            if( 'free_shipping' === $rate->method_id  ){
                unset($rates[$rate_key]);
            }
        }
    }
    return $rates;
}

Don't forget after saving that code to your theme's functions.php file to refresh your shipping rates in Admin shipping rates settings, disabling and save any shipping method and re-enable and save it back…