将 Woocommerce 中的免费送货优惠券的所有送货方式成本设置为零

4 php wordpress coupon woocommerce

我的购物车中有 3 种运输方式,一旦您的客户输入免费送货优惠券,这些方式的价格就应该为零。

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

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

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

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

运输类别不是计算的,而是由产品设置的,我使用表费率插件来计算重量。

Loi*_*tec 5

必须使用选项“有效的免费送货优惠券”\xe2\x80\xa6 启用第一种免费送货方式

\n\n

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

\n\n

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

\n\n
\n

更新:隐藏“免费送货”方法并附加带有(free)”的运输标签标题

\n
\n\n
add_filter( \'woocommerce_package_rates\', \'coupon_free_shipping_customization\', 20, 2 );\nfunction coupon_free_shipping_customization( $rates, $package ) {\n    $has_free_shipping = false;\n\n    $applied_coupons = WC()->cart->get_applied_coupons();\n    foreach( $applied_coupons as $coupon_code ){\n        $coupon = new WC_Coupon($coupon_code);\n        if($coupon->get_free_shipping()){\n            $has_free_shipping = true;\n            break;\n        }\n    }\n\n    foreach( $rates as $rate_key => $rate ){\n        if( $has_free_shipping ){\n            // For "free shipping" method (enabled), remove it\n            if( $rate->method_id == \'free_shipping\'){\n                unset($rates[$rate_key]);\n            }\n            // For other shipping methods\n            else {\n                // Append rate label titles (free)\n                $rates[$rate_key]->label .= \' \' . __(\'(free)\', \'woocommerce\');\n\n                // Set rate cost\n                $rates[$rate_key]->cost = 0;\n\n                // Set taxes rate cost (if enabled)\n                $taxes = array();\n                foreach ($rates[$rate_key]->taxes as $key => $tax){\n                    if( $rates[$rate_key]->taxes[$key] > 0 )\n                        $taxes[$key] = 0;\n                }\n                $rates[$rate_key]->taxes = $taxes;\n            }\n        }\n    }\n    return $rates;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

经过测试并有效。它应该也适合你。

\n\n
\n

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

\n
\n