基于运费 class 和 Woocommerce 中的最低金额的有条件免费送货
Conditional free shipping based on shipping class and minimal amount in Woocommerce
在关于运输方式的 woocommerce 中,我试图拥有以下内容:
- 购物车中只有产品 A:设置为 "Free shipping"
- 购物车中只有产品 B:设置为:
- 如果产品 B 的购买量少于 200,则固定费率金额为 15
- 如果产品 B 的购买量达到或超过 200 个,则免运费..
- 产品 A + 产品 B 同时在购物车中:"Free Shipping" 没有任何数量限制。
我已经尝试过使用固定费率和运费 类 我觉得如果产品 A 和产品 B 在那里,那么如果购物车没有达到 200,它会收取 15 的运费。
感谢任何帮助。
已更新: 要使其首先运行,您需要:
- 添加“免运费”class(第一个),
- 要启用 2 种送货方式:“免费送货”和“固定费率”,
- 在免运费的产品中需要设置含运费class“免费”、
- 在您的其他产品中将没有任何定义的运费class。
对于“免费送货”方式,您不会对其添加任何限制金额。
对于“统一费率”送货方式,您将按照此屏幕截图中的方式进行设置:
魔术将由以下代码完成,剩下的就是:
add_filter('woocommerce_package_rates', 'conditional_free_shipping', 10, 2);
函数 conditional_free_shipping( $rates, $package ){
如果 ( is_admin() && !defined( 'DOING_AJAX' ) )
return $费率;
## -- Your settings below -- ##
$shipping_class = 'free'; // "Free" shipping class products
$min_free_amount = 200; // Minimal Free shipping amount for normal products
## -- -- -- -- -- -- -- -- -- ##
$has_free = false; // Initializing
$products_total = 0; // Initializing
// Loop through cart items
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_shipping_class() == $shipping_class ) {
$has_free = true;
} else {
// Get the total purchased amount for normal product
$products_total += $cart_item['line_total'] + $cart_item['line_tax'];
}
}
foreach ( $rates as $rate_key => $rate ){
// 1. Only Free shipping products in cart OR both products kind in cart
if( $has_free ) {
if( 'flat_rate' === $rate->method_id )
unset( $rates[$rate_key] ); // Remove flat rate
}
// 2. Only normal products in cart
else {
// A. If it's under the min amount
if( 'free_shipping' === $rate->method_id && $products_total < $min_free_amount )
unset( $rates[$rate_key] ); // Remove Free shipping
// B. When min amount is reached
elseif( 'flat_rate' === $rate->method_id && $products_total >= $min_free_amount )
unset( $rates[$rate_key] ); // Remove flat rate
}
}
return $rates;
}
代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。
You might need to refresh shipping cached data: disable, save and enable, save related shipping methods for the current shipping zone, in Woocommerce shipping settings.
我对代码做了一些改进,现在它工作正常..
add_filter('woocommerce_package_rates', 'conditional_free_shipping', 10, 2);
function conditional_free_shipping( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
## -- Your settings bellow -- ##
$shipping_class = 'free'; // "Free" shipping class products
$min_free_amount = 200; // Minimal Free shipping amount for normal products
## -- -- -- -- -- -- -- -- -- ##
$has_normal = $has_free = false; // Initializing
$products_total = 0; // Initializing
// Loop through cart items
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_shipping_class() == $shipping_class ) {
$has_free = true;
} else {
$has_normal = true;
// Get the total purchased amount for normal product
$products_total += $cart_item['line_total'] + $cart_item['line_tax'];
}
}
foreach ( $rates as $rate_key => $rate ){
// 1. Only Free shipping products in cart
if( $has_free && ! $has_normal ) {
if( 'flat_rate' === $rate->method_id )
unset( $rates[$rate_key] ); // Remove flat rate
}
elseif(( $has_free && $has_normal )){
if( 'flat_rate' === $rate->method_id && $products_total <= $min_free_amount )
unset( $rates[$rate_key] );
}
// 2. Only normal products in cart OR Both products kind in cart
elseif( ( ! $has_free && $has_normal ) ) {
// A. If it's under the min amount
if( 'free_shipping' === $rate->method_id && $products_total < $min_free_amount )
unset( $rates[$rate_key] ); // Remove Free shipping
// B. When min amount is reached
elseif( 'flat_rate' === $rate->method_id && $products_total >= $min_free_amount )
unset( $rates[$rate_key] ); // Remove flat rate
}
}
return $rates;
}
在关于运输方式的 woocommerce 中,我试图拥有以下内容:
- 购物车中只有产品 A:设置为 "Free shipping"
- 购物车中只有产品 B:设置为:
- 如果产品 B 的购买量少于 200,则固定费率金额为 15
- 如果产品 B 的购买量达到或超过 200 个,则免运费..
- 产品 A + 产品 B 同时在购物车中:"Free Shipping" 没有任何数量限制。
我已经尝试过使用固定费率和运费 类 我觉得如果产品 A 和产品 B 在那里,那么如果购物车没有达到 200,它会收取 15 的运费。
感谢任何帮助。
已更新: 要使其首先运行,您需要:
- 添加“免运费”class(第一个),
- 要启用 2 种送货方式:“免费送货”和“固定费率”,
- 在免运费的产品中需要设置含运费class“免费”、
- 在您的其他产品中将没有任何定义的运费class。
对于“免费送货”方式,您不会对其添加任何限制金额。
对于“统一费率”送货方式,您将按照此屏幕截图中的方式进行设置:
魔术将由以下代码完成,剩下的就是:
add_filter('woocommerce_package_rates', 'conditional_free_shipping', 10, 2); 函数 conditional_free_shipping( $rates, $package ){ 如果 ( is_admin() && !defined( 'DOING_AJAX' ) ) return $费率;
## -- Your settings below -- ## $shipping_class = 'free'; // "Free" shipping class products $min_free_amount = 200; // Minimal Free shipping amount for normal products ## -- -- -- -- -- -- -- -- -- ## $has_free = false; // Initializing $products_total = 0; // Initializing // Loop through cart items foreach( $package['contents'] as $cart_item ) { if( $cart_item['data']->get_shipping_class() == $shipping_class ) { $has_free = true; } else { // Get the total purchased amount for normal product $products_total += $cart_item['line_total'] + $cart_item['line_tax']; } } foreach ( $rates as $rate_key => $rate ){ // 1. Only Free shipping products in cart OR both products kind in cart if( $has_free ) { if( 'flat_rate' === $rate->method_id ) unset( $rates[$rate_key] ); // Remove flat rate } // 2. Only normal products in cart else { // A. If it's under the min amount if( 'free_shipping' === $rate->method_id && $products_total < $min_free_amount ) unset( $rates[$rate_key] ); // Remove Free shipping // B. When min amount is reached elseif( 'flat_rate' === $rate->method_id && $products_total >= $min_free_amount ) unset( $rates[$rate_key] ); // Remove flat rate } } return $rates;
}
代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。
You might need to refresh shipping cached data: disable, save and enable, save related shipping methods for the current shipping zone, in Woocommerce shipping settings.
我对代码做了一些改进,现在它工作正常..
add_filter('woocommerce_package_rates', 'conditional_free_shipping', 10, 2);
function conditional_free_shipping( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
## -- Your settings bellow -- ##
$shipping_class = 'free'; // "Free" shipping class products
$min_free_amount = 200; // Minimal Free shipping amount for normal products
## -- -- -- -- -- -- -- -- -- ##
$has_normal = $has_free = false; // Initializing
$products_total = 0; // Initializing
// Loop through cart items
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_shipping_class() == $shipping_class ) {
$has_free = true;
} else {
$has_normal = true;
// Get the total purchased amount for normal product
$products_total += $cart_item['line_total'] + $cart_item['line_tax'];
}
}
foreach ( $rates as $rate_key => $rate ){
// 1. Only Free shipping products in cart
if( $has_free && ! $has_normal ) {
if( 'flat_rate' === $rate->method_id )
unset( $rates[$rate_key] ); // Remove flat rate
}
elseif(( $has_free && $has_normal )){
if( 'flat_rate' === $rate->method_id && $products_total <= $min_free_amount )
unset( $rates[$rate_key] );
}
// 2. Only normal products in cart OR Both products kind in cart
elseif( ( ! $has_free && $has_normal ) ) {
// A. If it's under the min amount
if( 'free_shipping' === $rate->method_id && $products_total < $min_free_amount )
unset( $rates[$rate_key] ); // Remove Free shipping
// B. When min amount is reached
elseif( 'flat_rate' === $rate->method_id && $products_total >= $min_free_amount )
unset( $rates[$rate_key] ); // Remove flat rate
}
}
return $rates;
}