运费根据购物车商品重量和购物车金额计算
Shipping calculated on cart items weight and cart amount
在客户端 WooCommerce 网站中,订单金额不超过 250 件可以免费送货。我使用下面的代码 (来自 ) ,隐藏订单金额超过 250 时的其他运费,除非购物车中有重物。
add_filter( 'woocommerce_package_rates', 'conditionally_hide_other_shipping_based_on_items_weight', 100, 1 );
function conditionally_hide_other_shipping_based_on_items_weight( $rates ) {
// targeted weight
$target_product_weight = 12;
$target_cart_amount = 250;
WC()->cart->subtotal_ex_tax >= $target_cart_amount ? $passed = true : $passed = false ;
// Iterating trough cart items to get the weight for each item
foreach(WC()->cart->get_cart() as $cart_item){
if( $cart_item['variation_id'] > 0)
$item_id = $cart_item['variation_id'];
else
$item_id = $cart_item['product_id'];
// Getting the product weight
$product_weight = get_post_meta( $item_id , '_weight', true);
if( !empty($product_weight) && $product_weight >= $target_cart_amount ){
$light_products_only = false;
break;
}
else $light_products_only = true;
}
// If 'free_shipping' method is available and if products are not heavy
// and cart amout up to the target limit, we hide other methods
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id && $passed && $light_products_only ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
但是现在,我想设置一个可变的运费,它将以两种方式计算:
- 当订单金额低于250时,订单总重量按公斤计算1欧元。
- 当订单金额达到250时,将只计算重件重量(1欧元每公斤)。如果没有重物,可以包邮。
有点复杂,我该如何实现?
有什么要遵循的曲目吗?
我已经尝试了一些现有的相关插件,但它们对这种情况并不方便。
谢谢。
代码改进 (2019 年 3 月):
是的,如果没有带有自定义 运费 的插件,这是可能的,根据购物车商品重量和购物车金额计算……但是您需要 'Flat rate'
送货方式设置最低金额。这通常应该适用于您正在使用的代码。
这是代码 (已评论):
//Adding a custom Shipping Fee to cart based conditionaly on weight and cart amount
add_action('woocommerce_cart_calculate_fees', 'custom_conditional_shipping_fee', 10, 1);
function custom_conditional_shipping_fee( $cart ){
## --- YOUR SETTINGS --- ##
$targeted_weight = 20; // Your targeted "heavy" product weight
$targeted_cart_amount = 250; // Your targeted cart amount
$price_per_kg = 1; // Price by Kg;
$flat_rate_price = 10; // Set the cost like in 'flat rate' shipping method
// Initializing variables
$fee = $calculated_weight = 0;
// For cart SUBTOTAL amount EXCLUDING TAXES
$passed = $cart->subtotal_ex_tax >= $targeted_cart_amount ? true : false ;
// For cart SUBTOTAL amount INCLUDING TAXES (replace by this):
// $passed = $cart->subtotal >= $targeted_cart_amount ? true : false ;
// Iterating through each cart items
foreach( $cart->get_cart() as $cart_item ){
// The Product weight
$product_weight = $cart_item['data']->get_weight();
// cart item weight
$cart_item_weight = $cart_item['quantity'] * $cart_item['data']->get_weight();
// When cart amount is up to 250, Adding weight of heavy items
if($passed && $product_weight > $targeted_weight)
$calculated_weight += $cart_item_weight;
}
#### Making the fee calculation ####
// Cart is up to 250 with heavy items
if ( $passed && $calculated_weight != 0 ) {
// Fee is based on cumulated weight of heavy items
$fee = ( $calculated_weight * $price_per_kg ) - $flat_rate_price;
}
// Cart is below 250
elseif ( ! $passed ) {
// Fee is based on cart total weight
$fee = ( $cart->get_cart_contents_weight( ) * $price_per_kg ) - $flat_rate_price;
}
#### APPLYING THE CALCULATED FEE ####
// When cart is below 250 or when there is heavy items
if ($fee > 0){
// Rounding the fee
$fee = round( $fee );
// This shipping fee is taxable (You can have it not taxable changing last argument to false)
$cart->add_fee( __('Shipping weight fee', 'woocommerce'), $fee, true);
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效
The minimal 'Flat rate' amount is subtracted from the fee calculation, this way the customer pay the right price.
在客户端 WooCommerce 网站中,订单金额不超过 250 件可以免费送货。我使用下面的代码 (来自
add_filter( 'woocommerce_package_rates', 'conditionally_hide_other_shipping_based_on_items_weight', 100, 1 );
function conditionally_hide_other_shipping_based_on_items_weight( $rates ) {
// targeted weight
$target_product_weight = 12;
$target_cart_amount = 250;
WC()->cart->subtotal_ex_tax >= $target_cart_amount ? $passed = true : $passed = false ;
// Iterating trough cart items to get the weight for each item
foreach(WC()->cart->get_cart() as $cart_item){
if( $cart_item['variation_id'] > 0)
$item_id = $cart_item['variation_id'];
else
$item_id = $cart_item['product_id'];
// Getting the product weight
$product_weight = get_post_meta( $item_id , '_weight', true);
if( !empty($product_weight) && $product_weight >= $target_cart_amount ){
$light_products_only = false;
break;
}
else $light_products_only = true;
}
// If 'free_shipping' method is available and if products are not heavy
// and cart amout up to the target limit, we hide other methods
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id && $passed && $light_products_only ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
但是现在,我想设置一个可变的运费,它将以两种方式计算:
- 当订单金额低于250时,订单总重量按公斤计算1欧元。
- 当订单金额达到250时,将只计算重件重量(1欧元每公斤)。如果没有重物,可以包邮。
有点复杂,我该如何实现?
有什么要遵循的曲目吗?
我已经尝试了一些现有的相关插件,但它们对这种情况并不方便。
谢谢。
代码改进 (2019 年 3 月):
是的,如果没有带有自定义 运费 的插件,这是可能的,根据购物车商品重量和购物车金额计算……但是您需要 'Flat rate'
送货方式设置最低金额。这通常应该适用于您正在使用的代码。
这是代码 (已评论):
//Adding a custom Shipping Fee to cart based conditionaly on weight and cart amount
add_action('woocommerce_cart_calculate_fees', 'custom_conditional_shipping_fee', 10, 1);
function custom_conditional_shipping_fee( $cart ){
## --- YOUR SETTINGS --- ##
$targeted_weight = 20; // Your targeted "heavy" product weight
$targeted_cart_amount = 250; // Your targeted cart amount
$price_per_kg = 1; // Price by Kg;
$flat_rate_price = 10; // Set the cost like in 'flat rate' shipping method
// Initializing variables
$fee = $calculated_weight = 0;
// For cart SUBTOTAL amount EXCLUDING TAXES
$passed = $cart->subtotal_ex_tax >= $targeted_cart_amount ? true : false ;
// For cart SUBTOTAL amount INCLUDING TAXES (replace by this):
// $passed = $cart->subtotal >= $targeted_cart_amount ? true : false ;
// Iterating through each cart items
foreach( $cart->get_cart() as $cart_item ){
// The Product weight
$product_weight = $cart_item['data']->get_weight();
// cart item weight
$cart_item_weight = $cart_item['quantity'] * $cart_item['data']->get_weight();
// When cart amount is up to 250, Adding weight of heavy items
if($passed && $product_weight > $targeted_weight)
$calculated_weight += $cart_item_weight;
}
#### Making the fee calculation ####
// Cart is up to 250 with heavy items
if ( $passed && $calculated_weight != 0 ) {
// Fee is based on cumulated weight of heavy items
$fee = ( $calculated_weight * $price_per_kg ) - $flat_rate_price;
}
// Cart is below 250
elseif ( ! $passed ) {
// Fee is based on cart total weight
$fee = ( $cart->get_cart_contents_weight( ) * $price_per_kg ) - $flat_rate_price;
}
#### APPLYING THE CALCULATED FEE ####
// When cart is below 250 or when there is heavy items
if ($fee > 0){
// Rounding the fee
$fee = round( $fee );
// This shipping fee is taxable (You can have it not taxable changing last argument to false)
$cart->add_fee( __('Shipping weight fee', 'woocommerce'), $fee, true);
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效
The minimal 'Flat rate' amount is subtracted from the fee calculation, this way the customer pay the right price.