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

问题描述

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

现在,如果客户的购物车包含 - 假设为 48 欧元 - 他无需支付运费,因为他已达到申请 free_shipping 的订单总额。

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

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

基于 Applied coupons disable Free shipping conditionally in Woocommerce 答案代码,这是我的代码尝试:

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 文件中。它应该可以工作。

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...