Woocommerce 3 中基于购物车总重量的运费
Shipping cost based on cart total weight in Woocommerce 3
在我的 Woocommerce 网上商店中,我有不同的产品。我想根据购物车商品总重量计算运费:
- 从
0
到 6
公斤的成本是 5 €
,
- 从
6
到 12
公斤的成本是 9 €
实际上,如果我有一个 1 公斤的产品,运费是 5 €
,但如果我有 10我购物车里的这个商品,运费还是5 €
(应该是9 €
代替)。
如何根据购物车总重量计算累进运费?
尝试以下功能,"Flat Rate" 成本将根据购物车总重量进行更改。
使用"Flate rate"送货方式,您需要使用简单的初始成本而不是任何公式来设置参考运费。例如,它可以是 1
。此费用将由我的答案代码替换,动态基于购物车总重量。
You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.
add_filter('woocommerce_package_rates', 'shipping_cost_based_on_weight', 12, 2);
function shipping_cost_based_on_weight( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE define the differents costs
$cost1 = 5; // Up to 6 Kg
$cost2 = 9; // Above 6 Kg and below 12 kg
$cost3 = 9; // Above 12 kg
// The cart total Weight
$total_weight = WC()->cart->get_cart_contents_weight();
// Loop through the shipping taxes array
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targetting "flat rate"
if( 'flat_rate' === $rate->method_id ){
// Get the initial cost
$initial_cost = $new_cost = $rates[$rate_key]->cost;
// Calculate new cost
if( $total_weight <= 6 ) { // Below 6 Kg
$new_cost = $cost1;
}
elseif( $total_weight > 6 && $total_weight <= 12 ) { // Between 6 and 12 Kg
$new_cost = $cost2;
}
else { // Above 12 Kg
$new_cost = $cost3;
}
// Set the new cost
$rates[$rate_key]->cost = $new_cost;
// Taxes rate cost (if enabled)
$taxes = [];
// Loop through the shipping taxes array (as they can be many)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
// Get the initial tax cost
$initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
// Get the tax rate conversion
$tax_rate = $initial_tax_cost / $initial_cost;
// Set the new tax cost
$taxes[$key] = $new_cost * $tax_rate;
$has_taxes = true; // Enabling tax
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
Once tested, don't forget to disable "Enable debug mode" option in shipping settings.
在我的 Woocommerce 网上商店中,我有不同的产品。我想根据购物车商品总重量计算运费:
- 从
0
到6
公斤的成本是5 €
, - 从
6
到12
公斤的成本是9 €
实际上,如果我有一个 1 公斤的产品,运费是 5 €
,但如果我有 10我购物车里的这个商品,运费还是5 €
(应该是9 €
代替)。
如何根据购物车总重量计算累进运费?
尝试以下功能,"Flat Rate" 成本将根据购物车总重量进行更改。
使用"Flate rate"送货方式,您需要使用简单的初始成本而不是任何公式来设置参考运费。例如,它可以是 1
。此费用将由我的答案代码替换,动态基于购物车总重量。
You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.
add_filter('woocommerce_package_rates', 'shipping_cost_based_on_weight', 12, 2);
function shipping_cost_based_on_weight( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE define the differents costs
$cost1 = 5; // Up to 6 Kg
$cost2 = 9; // Above 6 Kg and below 12 kg
$cost3 = 9; // Above 12 kg
// The cart total Weight
$total_weight = WC()->cart->get_cart_contents_weight();
// Loop through the shipping taxes array
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targetting "flat rate"
if( 'flat_rate' === $rate->method_id ){
// Get the initial cost
$initial_cost = $new_cost = $rates[$rate_key]->cost;
// Calculate new cost
if( $total_weight <= 6 ) { // Below 6 Kg
$new_cost = $cost1;
}
elseif( $total_weight > 6 && $total_weight <= 12 ) { // Between 6 and 12 Kg
$new_cost = $cost2;
}
else { // Above 12 Kg
$new_cost = $cost3;
}
// Set the new cost
$rates[$rate_key]->cost = $new_cost;
// Taxes rate cost (if enabled)
$taxes = [];
// Loop through the shipping taxes array (as they can be many)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
// Get the initial tax cost
$initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
// Get the tax rate conversion
$tax_rate = $initial_tax_cost / $initial_cost;
// Set the new tax cost
$taxes[$key] = $new_cost * $tax_rate;
$has_taxes = true; // Enabling tax
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
Once tested, don't forget to disable "Enable debug mode" option in shipping settings.