WooCommerce购物车超1200捷克克朗时设置运费为零的代码需求
WooCommerce 购物车满额免运费(保留配送选项)方案
以下代码可实现当购物车金额超过1200捷克克朗(CZK)时,将所有配送方式的费用设为0,同时完整保留所有配送选项:
add_filter( 'woocommerce_package_rates', 'custom_free_shipping_over_threshold', 100, 2 ); function custom_free_shipping_over_threshold( $rates, $package ) { // 免运费阈值(单位:CZK) $threshold = 1200; // 获取购物车小计(不含税,如需含税可替换为 WC()->cart->get_total()) $cart_subtotal = WC()->cart->get_subtotal(); // 当前商店货币代码 $currency = get_woocommerce_currency(); // 仅在CZK货币且购物车金额达标时生效 if ( $currency === 'CZK' && $cart_subtotal >= $threshold ) { foreach ( $rates as $rate_key => $rate ) { // 重置配送基础费用为0 $rates[$rate_key]->cost = 0; // 重置配送税费为0(如果存在税费) if ( isset( $rates[$rate_key]->taxes ) && is_array( $rates[$rate_key]->taxes ) ) { foreach ( $rates[$rate_key]->taxes as $tax_key => $tax_amount ) { $rates[$rate_key]->taxes[$tax_key] = 0; } } } } return $rates; }
关键说明
- 钩子选择:使用
woocommerce_package_rates核心钩子,设置优先级100确保能覆盖其他费率修改逻辑 - 精准触发:通过货币判断仅对CZK生效,避免影响其他货币场景;购物车金额可按需选择不含税小计或含税总价
- 完整免运费:同时重置配送基础费用和关联税费,确保用户看到的最终配送成本为0
- 保留配送选项:仅修改费用数值,不隐藏任何配送方式,完全适配你的需求
使用方法
- 将代码添加到WordPress主题的
functions.php文件末尾,或通过自定义代码插件(如Code Snippets)添加 - 保存后前往WooCommerce设置→配送页面,点击「保存更改」刷新配送缓存
- 测试验证:添加商品至购物车,当金额超过1200CZK时,查看配送选项是否全部显示为0费用
内容的提问来源于stack exchange,提问作者David Venc
相关产品推荐
相关产品推荐

