【问题标题】:Enable free shipping for a min amount based on WooCommerce discounted subtotal根据 WooCommerce 折扣小计启用最低金额的免费送货
【发布时间】:2021-07-05 04:29:55
【问题描述】:

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

现在,如果客户有一个购物车 - 比如说 48 欧元 - 他不必支付运费,因为他已达到订单总额以申请免费送货。

如果他现在申请了 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;
}

【问题讨论】:

  • 对不起,我正在处理在这个平台上找到的一些代码,但最终无法完成。

标签: php wordpress woocommerce coupon shipping-method


【解决方案1】:

更新

首先在免费送货的送货设置中,您需要将最低金额设置为 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 文件中。它应该可以工作。

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

【讨论】:

  • 已测试:购物车 sub_total 为 45.40 欧元,因此应用免费送货,因此 cart_total 也是 45.40 欧元。但是,一旦我申请了 10 欧元的优惠券并且我们的总价低于 45 欧元,免费送货就消失了,我们的统一费率 4,95 欧元的运费再次到位 -> 购物车总价为 39,85 欧元。跨度>
猜你喜欢
  • 2020-11-26
  • 1970-01-01
  • 2016-11-05
  • 2014-12-22
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多