WooCommerce - 如果运输类别为真且数量超过某个数字,则免费送货

时间:2017-06-06 03:14:12

标签: wordpress woocommerce

我的购物车需要免费送货,如果有特定产品的货运类别,并且该产品的数量超过8.如果购物车上有其他产品,则无关紧要。运费将是免费的。

因为我现在正在寻找货运类(这是正确的),但计算整个购物车的数量。不是购物车上有特定产品的特定产品。我怎么能用PHP做到这一点?

1 个答案:

答案 0 :(得分:0)

add_filter('woocommerce_package_rates', 'show_hide_free_shipping', 10, 2);
function show_hide_free_shipping($available_shipping_methods, $package){
    $minimum_number_of_item = 12; //Give here minumum number of items to allow freeshipping
    $shop_country = 'AU'; //Give here the country code where the store located

    $free_shipping  =   false;
    global $woocommerce;
    $customer_country = $woocommerce->customer->get_shipping_country();

    $item_count = 0;
    foreach (WC()->cart->cart_contents as $key => $item) {
        $item_count += $item['quantity'];
    }

    if( $item_count >= $minimum_number_of_item && $customer_country ==  $shop_country ){
        $free_shipping  =   true;
    }

    if($free_shipping){
        foreach($available_shipping_methods as $shipping_method =>  $method){
            if( strpos( $shipping_method, 'free_shipping' ) === false ) {
                unset($available_shipping_methods[$shipping_method]);
            }
        }
    }
    else{
        foreach($available_shipping_methods as $shipping_method =>  $method){
            if( strpos( $shipping_method, 'free_shipping' ) !== false ) {
                unset($available_shipping_methods[$shipping_method]);
            }
        }
    }
    return $available_shipping_methods;
}

来自以下链接。 你可以通过一些调整来使用它。

https://www.xadapter.com/showhide-free-shipping-based-item-count-domestic-shipment/

https://www.xadapter.com/hide-shipping-methods-shipping-classes-exist-not-exist-cart/