【发布时间】:2017-02-22 16:16:15
【问题描述】:
在客户 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;
}
但是现在,我想设置一个可变的运费金额,它将以两种方式计算:
- 当订单金额低于 250 时,订单商品总重量按公斤计算 1 欧元。
- 当订单金额达到 250 件时,将仅计算重物重量(1 欧元/公斤)。如果没有重物,可享受免运费服务。
我怎样才能做到这一点,因为它有点复杂?
有什么可以追的吗?
我已经尝试了一些现有的相关插件,但它们不适合这种情况。
谢谢。
【问题讨论】:
标签: php wordpress woocommerce cart checkout