在客户端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;
}
但是现在,我想设置一个可变运费金额,将以两种方式计算:
我怎样才能做到这一点,因为它有点复杂?
任何跟踪的轨道?
我尝试了一些现有的相关插件,但对于这种情况它们并不方便。
感谢。