1
在客户端WooCommerce网站,免费送货方式启用订单金额高达250.我使用代码(从this answer),以隐藏其他运费时,订单金额超过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;
}
但是现在,我想设置将在2种方式计算的变量出厂量:
- 当顺序量低于250时,计算将1欧元由千对总订单项重量。
- 当订单金额达到250时,只会计算重物重量(每公斤1欧元)。如果没有重物品,可以免费送货。
我该如何做到这一点,因为它有点复杂?
任何跟踪?
我已经尝试了一些现有的相关插件,但它们对于这种情况并不方便。
谢谢。
非常感谢! – spy