根据 WooCommerce 中的购物车项目小计,将所有运输方式成本设置为零
Set all shipping methods costs to zero based on cart items subtotal in WooCommerce
我的页面上有多种送货方式,当订单超过 2500 捷克克朗时,我希望它们的成本为零。我只发现代码仅在客户达到免费送货的最低限度时才显示免费送货选项,但这不是我想要的。我想显示所有方法(但价格为零),以便客户仍然可以选择哪种送货方式他希望他的订单与他一起交付的公司。
谢谢!
基于 答案代码,以下是根据特定购物车商品小计金额使其工作的方法:
add_filter('woocommerce_package_rates', 'null_shipping_costs_conditionally', 10, 2 );
function null_shipping_costs_conditionally( $rates, $package ){
$threshold_amount = 2500;
if( $package['contents_cost'] >= $threshold_amount ) {
// Loop through shipping methods rates
foreach ( $rates as $rate_key => $rate ){
// Targeting all shipping methods except "Free shipping"
if ( 'free_shipping' !== $rate->method_id ) {
$has_taxes = false;
$taxes = [];
$rates[$rate_key]->cost = 0; // Set cost to 0 (zero)
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
$has_taxes = true;
$taxes[$key] = 0; // Set tax cost to 0 (zero)
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
注意:保存此代码后,请不要忘记清空购物车以刷新运输缓存数据。
我的页面上有多种送货方式,当订单超过 2500 捷克克朗时,我希望它们的成本为零。我只发现代码仅在客户达到免费送货的最低限度时才显示免费送货选项,但这不是我想要的。我想显示所有方法(但价格为零),以便客户仍然可以选择哪种送货方式他希望他的订单与他一起交付的公司。 谢谢!
基于
add_filter('woocommerce_package_rates', 'null_shipping_costs_conditionally', 10, 2 );
function null_shipping_costs_conditionally( $rates, $package ){
$threshold_amount = 2500;
if( $package['contents_cost'] >= $threshold_amount ) {
// Loop through shipping methods rates
foreach ( $rates as $rate_key => $rate ){
// Targeting all shipping methods except "Free shipping"
if ( 'free_shipping' !== $rate->method_id ) {
$has_taxes = false;
$taxes = [];
$rates[$rate_key]->cost = 0; // Set cost to 0 (zero)
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
$has_taxes = true;
$taxes[$key] = 0; // Set tax cost to 0 (zero)
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
注意:保存此代码后,请不要忘记清空购物车以刷新运输缓存数据。