问题描述
使用 WooCommerce,我需要获得超过 250 的免费送货服务,但购物车中包含的重型产品除外.
With WooCommerce, I need to have Free Shipping over certain amount of 250 except the heavy products that are included in the cart.
有人知道我该怎么做吗?
Does anyone know what i should do?
谢谢.
推荐答案
此自定义代码将保留免费送货方式,并在购物车金额达到250时隐藏其他送货方式 并且如果产品不重(此处小于 20 公斤)……如果订单少于 250 公斤,则不允许免费送货,您可以在 woocommerce 中进行设置(见文末).
This custom code will keep free shipping method and will hide other shipping methods when the cart amount is up to 250 and if products are not heavy (less than 20 kg here)… To not allow Free shipping for orders less than 250, you can set this in woocommerce (see at the end).
首先,您必须确保在每个重型产品中设置了重量(对于简单或可变产品(在每个变体中).这里的购物车小计是不含税(并且您可以轻松地将其更改为包括税金).
First you will have to sure that the weight is set in each heavy product (for simple or variables products (in each variations). The cart subtotal here is Excluding taxes (and you can change it to Including taxes easily).
然后是 woocommerce_package_rates
过滤器钩子中的自定义钩子函数:
Then here is that custom hooked function in woocommerce_package_rates
filter hook:
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 ) {
// Set HERE your targeted weight (here is 20 kg) <== <== <== <== <==
$targeted_product_weight = 20;
// Set HERE your targeted cart amount (here is 250) <== <== <== <== <==
$targeted_cart_amount = 250;
// For cart subtotal amount EXCLUDING TAXES
$passed = WC()->cart->subtotal_ex_tax >= $targeted_cart_amount ? true : false;
// For cart subtotal amount INCLUDING TAXES (replace by this):
// $passed = WC()->cart->subtotal >= $targeted_cart_amount ? true : false;
$light_products_only = true; // Initializing
// Iterating trough cart items to get the weight for each item
foreach( $package['contents'] as $cart_item ){
// Getting the product weight
$product_weight = $cart_item['data']->get_weight();
if( !empty($product_weight) && $product_weight >= $targeted_product_weight ){
$light_products_only = false;
break;
}
}
// 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;
}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
此代码经过测试,适用于简单和可变的产品......
This code is tested and it works for simple and variable products…
您还必须在 WooCommerce 设置中设置 >送货,对于每个送货区域和 免费送货"
方法,您的最低订购量:
您需要刷新运输缓存数据:在woocommerce运输设置中禁用、保存和启用、保存当前运输区域的相关运输方式.
这篇关于根据重量和最小购物车数量免费送货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!