Woocommerce 运输用例
Woocommerce shipping use case
我有一个网站用例,但我不知道如何配置 WooCommerce Shipping 来执行我想要的操作。
关于该商店的运费:
- 有些产品只能本地送货(免费)。如果有人在规定的区域之外,应该有一条消息通知他这是唯一的本地产品。
- 其他产品可以送货到当地(免费)或外地。
- 这些可以外送的商品,运费不同(商品A+$25,商品B+$30,商品C+$10...)
我设置了两个运输区域:
- 本地(仅邮政编码列表)-> 免费,
- 外部区域(到处)-> 放一堆 classes 例如 +10 , +20 , +25 ... 给他们 rates
我这里有两个主要问题:
- 我不知道如何强制某些产品为本地产品。
- 在进行上述设置时,如果有人尝试从本地邮政编码结帐,则我为其发货 class 的外部区域的产品不会在本地显示。
知道如何解决这个问题吗?
由于您已启用免费本地运费,因此您需要将其设置为以下代码。它可以是 "Free Shipping" 方法或 "Local Pickup" 方法(设置为 0 成本和无税)。
您还必须在函数中设置需要启用此送货方式(隐藏所有其他方式)的产品 ID 数组。
add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 10, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
// Set HERE the shipping method to use:
// It can be 'free_shipping' or 'local_pickup' (set with 0 cost and no tax)
$shipping_method_id = 'local_pickup';
// Set HERE your "LOCAL free" product IDs
$local_free_ids = array( 40, 37 );
$found = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
if( in_array($cart_item[ 'product_id' ], $local_free_ids) ) { // <== ID OF MY SHIPPING_CLASS
$found = true;
break; // Stop the loop
}
}
// If on of this product Ids are in cart we hide all other shipping methods
if ( $found ){
$local_free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( $shipping_method_id === $rate->method_id ) {
$local_free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $local_free ) ? $local_free : $rates;
} else {
return $rates;
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
Sometimes is necessary to refresh the shipping caches:
1) First your cart is empty.
2) The code is already saved on your function.php file.
3) Go in a shipping zone and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done.
基于:WooCommerce - Hide other shipping methods when FREE SHIPPING is available
要仅通知本地产品的客户,应在新问题中询问...
这些有点值得商榷,但您无需编写代码即可完成此操作。因为我认为你已经在那里了。
I set up two Shipping zones:
Local (only a list of Zip codes) -> Free Rate, Outside Area
(everywhere) -> put a bunch of classes such as +10 , +20 , +25 ... and
gave them rates
在您的 Local
和 Everywhere
中应该有相同的运费 class 但应该有不同的值。
假设你的设置是这样的
配送区域 > 当地
区域:添加邮政编码或区域列表。
送货方式:添加统一运费方式
统一费率设置:
送货区>无处不在
区域:添加邮政编码或区域列表或留空。
送货方式:添加统一运费方式
统一费率设置:
I have 2 main problems here:
- I dont know how to force some products to be local ONLY.
- when doing the setup above, the products that are on the outside area for which I gave a shipping class are not showing in local if
someone is trying to checkout from a local Zip code.
- 你不知道。只需为产品添加正确的运费 class,运费将自行完成。
- 这就是你失去我的地方。这是我的购物车的屏幕截图。本地 vs 无处不在。
本地
无处不在
以上两张图片展示了具有“Class A”运费的产品 class。
我有一个网站用例,但我不知道如何配置 WooCommerce Shipping 来执行我想要的操作。
关于该商店的运费:
- 有些产品只能本地送货(免费)。如果有人在规定的区域之外,应该有一条消息通知他这是唯一的本地产品。
- 其他产品可以送货到当地(免费)或外地。
- 这些可以外送的商品,运费不同(商品A+$25,商品B+$30,商品C+$10...)
我设置了两个运输区域:
- 本地(仅邮政编码列表)-> 免费,
- 外部区域(到处)-> 放一堆 classes 例如 +10 , +20 , +25 ... 给他们 rates
我这里有两个主要问题:
- 我不知道如何强制某些产品为本地产品。
- 在进行上述设置时,如果有人尝试从本地邮政编码结帐,则我为其发货 class 的外部区域的产品不会在本地显示。
知道如何解决这个问题吗?
由于您已启用免费本地运费,因此您需要将其设置为以下代码。它可以是 "Free Shipping" 方法或 "Local Pickup" 方法(设置为 0 成本和无税)。
您还必须在函数中设置需要启用此送货方式(隐藏所有其他方式)的产品 ID 数组。
add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 10, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
// Set HERE the shipping method to use:
// It can be 'free_shipping' or 'local_pickup' (set with 0 cost and no tax)
$shipping_method_id = 'local_pickup';
// Set HERE your "LOCAL free" product IDs
$local_free_ids = array( 40, 37 );
$found = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
if( in_array($cart_item[ 'product_id' ], $local_free_ids) ) { // <== ID OF MY SHIPPING_CLASS
$found = true;
break; // Stop the loop
}
}
// If on of this product Ids are in cart we hide all other shipping methods
if ( $found ){
$local_free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( $shipping_method_id === $rate->method_id ) {
$local_free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $local_free ) ? $local_free : $rates;
} else {
return $rates;
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
Sometimes is necessary to refresh the shipping caches:
1) First your cart is empty.
2) The code is already saved on your function.php file.
3) Go in a shipping zone and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done.
基于:WooCommerce - Hide other shipping methods when FREE SHIPPING is available
要仅通知本地产品的客户,应在新问题中询问...
这些有点值得商榷,但您无需编写代码即可完成此操作。因为我认为你已经在那里了。
I set up two Shipping zones:
Local (only a list of Zip codes) -> Free Rate, Outside Area (everywhere) -> put a bunch of classes such as +10 , +20 , +25 ... and gave them rates
在您的 Local
和 Everywhere
中应该有相同的运费 class 但应该有不同的值。
假设你的设置是这样的
配送区域 > 当地
区域:添加邮政编码或区域列表。
送货方式:添加统一运费方式
统一费率设置:
送货区>无处不在
区域:添加邮政编码或区域列表或留空。
送货方式:添加统一运费方式
统一费率设置:
I have 2 main problems here:
- I dont know how to force some products to be local ONLY.
- when doing the setup above, the products that are on the outside area for which I gave a shipping class are not showing in local if someone is trying to checkout from a local Zip code.
- 你不知道。只需为产品添加正确的运费 class,运费将自行完成。
- 这就是你失去我的地方。这是我的购物车的屏幕截图。本地 vs 无处不在。
本地
无处不在
以上两张图片展示了具有“Class A”运费的产品 class。