根据 WooCommerce 折扣小计启用最低金额的免费送货

Enable free shipping for a min amount based on WooCommerce discounted subtotal

在 WooCommerce 中,我们已将 flat_rate 运费设置为 ​​4.95 欧元,free_shipping 显示的最低总金额为 45 欧元。

现在,如果客户的购物车 - 比方说 48 欧元 - 他不必支付运费,因为他已经达到要申请的订单总金额 free_shipping。

如果他现在申请 10% 的优惠券,他最终的订单总金额为 43.20 欧元,因此必须再次支付运费。

在该客户应用优惠券并“降到”free_shipping 金额以下后,我们仍然希望向该客户提供免费送货服务。否则使用 10% 的优惠券(在我们的例子中是 4.80 欧元)不是很有吸引力,但必须再次支付 4.95 欧元的运费。

基于 答案代码,这是我的代码尝试:

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

    
    $shipping_counrtry = WC()->customer->get_shipping_country();
    if ($shipping_counrtry == 'DE') :  $min_subtotal = 45;
    endif;
    $shipping_counrtry = WC()->customer->get_shipping_country();
    if ($shipping_counrtry == 'AT') :  $min_subtotal = 75;
    endif;
    
    
    // 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   = 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  ){
                // SET THE RATE HERE; but how 
            }
        }
    }
    return $rates;
}

已更新

首先在免费送货的送货设置中,您需要将最低金额设置为 0 (零)。然后以下代码将处理购物车商品非折扣小计以获得“免费送货”最小金额 (这将解决您的问题):

add_filter( 'woocommerce_package_rates', 'conditional_free_shipping', 10, 2 );
function conditional_free_shipping( $rates, $package ){
    $shipping_country = WC()->customer->get_shipping_country(); // Get shipping country
    $free_shipping = $other_rates = array(); // Initializing

    if ($shipping_country === 'DE') {
        $min_subtotal = 45;
    } elseif ($shipping_country === 'AT') {
        $min_subtotal = 75;
    }

    // Get subtotal incl tax (non discounted) for the current shipping package
    $items_subtotal  = array_sum( wp_list_pluck( $package['contents'], 'line_subtotal' ) );
    $items_subtotal += array_sum( wp_list_pluck( $package['contents'], 'line_subtotal_tax' ) );

    // Loop through shipping rates for current shipping package
    foreach ( $rates as $rate_key => $rate ){
        if( 'free_shipping' === $rate->method_id  ){
            $free_shipping[$rate_key] = $rate;
        } else
            $other_rates[$rate_key] = $rate;
        }
    }

    return isset($min_subtotal) && $items_subtotal >= $min_subtotal ? $free_shipping : $other_rates;
}

代码进入活动子主题(或活动主题)的 functions.php 文件。应该可以。

不要忘记清空购物车以刷新运输缓存数据。