将 Woocommerce 中的免费送货优惠券的所有送货方式成本设置为零 [英] Set all shipping methods cost to zero for a Free shipping coupon in Woocommerce

查看:39
本文介绍了将 Woocommerce 中的免费送货优惠券的所有送货方式成本设置为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的购物车中有 3 种送货方式,一旦您的客户输入 Free Shipping 优惠券,它们就会变成零价格.

I have 3 shipping methods in my cart that should become zero prices as soon as your customer enters Free Shipping coupon.

我知道如何在 functions.php 中添加过滤器来检测优惠券,但有人知道将购物车(单选按钮)中可见的送货方式设置为零的代码片段吗?

I know how to add a filter in functions.php to detect the coupon but is someone know a snippet to set shipping methods visibles in cart (radio button) to ZERO for this order?

我的送货方式是 UPS、FedEx 等公司...

My deliveries methods are companies like UPS, FedEx...

我激活了免费送货选项,以便可以使用优惠券进行管理.

I activated the free shipping option in order it can be managed with coupon.

客户选择的送货方式列表是根据我的产品运输等级和订单总重量计算的.

The list of choice of deliveries methods for the customers is calculated according to my products shipping class and order total weight.

运费等级不是计算的而是由产品设置的,我使用TABLE RATE PLUGIN 计算权重.

Shipping class are not calculated but set by product and i use TABLE RATE PLUGIN to calculate the weight.

非常感谢

推荐答案

必须启用第一种免费送货方式,并使用有效的免费送货优惠券"选项...

First free shipping method has to be enabled with option "A valid free shipping coupon"…

然后您需要设置所需的优惠券代码并启用允许免费送货"选项.

Then you need to set the desired coupon codes with option "Allow free shipping" enabled.

当应用有效的优惠券代码(启用允许免费送货"选项)时,以下代码会将所有送货方式成本设置为.

The following code will set all shipping methods costs to zero when a valid coupon code (with option "Allow free shipping" enabled) will be applied.

更新:隐藏免费送货"方法并附加带有(免费)"

Update: Hide "Free shipping" method and append shipping label titles with "(free)"

add_filter( 'woocommerce_package_rates', 'coupon_free_shipping_customization', 20, 2 );
function coupon_free_shipping_customization( $rates, $package ) {
    $has_free_shipping = false;

    $applied_coupons = WC()->cart->get_applied_coupons();
    foreach( $applied_coupons as $coupon_code ){
        $coupon = new WC_Coupon($coupon_code);
        if($coupon->get_free_shipping()){
            $has_free_shipping = true;
            break;
        }
    }

    foreach( $rates as $rate_key => $rate ){
        if( $has_free_shipping ){
            // For "free shipping" method (enabled), remove it
            if( $rate->method_id == 'free_shipping'){
                unset($rates[$rate_key]);
            }
            // For other shipping methods
            else {
                // Append rate label titles (free)
                $rates[$rate_key]->label .= ' ' . __('(free)', 'woocommerce');

                // Set rate cost
                $rates[$rate_key]->cost = 0;

                // Set taxes rate cost (if enabled)
                $taxes = array();
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 )
                        $taxes[$key] = 0;
                }
                $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.

经过测试并有效.它应该也适用于您.

Tested and works. It should works also for you.

有时,您可能需要刷新运输方式:
1)先清空购物车.
2) 进入送货区域设置,然后禁用/保存并重新启用/保存相关的送货方式.

Sometimes, you should may be need to refresh shipping methods:
1) Empty cart first.
2) Go to shipping Zones settings, then disable/save and re-enable/save the related shipping methods.

这篇关于将 Woocommerce 中的免费送货优惠券的所有送货方式成本设置为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆