当 WooCommerce 3 中提供免费送货时,隐藏具体统一费率

Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3

在 WooCommerce 3 中,我有这些送货选项(设置):

  1. 免运费:free_shipping:1 - 最低订购量设置为 </code>。</li> <li>正常运输 <code>flat_rate:3 - 金额 </code>。</li> <li>快递 <code>flat_rate:5 - 金额 </code>。</li> </ol> <p>我希望 <strong>快递</strong> 选项始终可用(如图所示)。 </p> <p>但是当 <strong>免费送货</strong> 可用时(意味着客户购物车中的商品超过 50 美元)我想隐藏 <strong>正常送货</strong> 只要。 </p> <p>因此,当 <strong><code>Free shipping 可用(和隐藏)时,可用运费将为 普通运输和快递运输。

    这可能吗?我怎样才能在 WooCommerce 3 上获得这个?

the official WooCommerce snippet code 的基础上,进行一些细微的更改,您将能够在免费送货时仅隐藏您的第一个固定费率:

add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
    // HERE yours 2nd flat rate "Express Shipping" (that you never hide) in the array:
    $flat_rates_express = array( 'flat_rate:5', 'flat_rate:12', 'flat_rate:14' );

    $free = $flat2 = array();
    foreach ( $rates as $rate_key => $rate ) {
        // Updated Here To 
        if ( in_array( $rate->id, $flat_rates_express ) )
            $flat2[ $rate_key ] = $rate;
        if ( 'free_shipping' === $rate->method_id )
            $free[ $rate_key ] = $rate;
    }
    return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

在 WooCommerce 3 上测试并有效。

Refresh the shipping caches:
1) First empty your cart.
2) This code is already saved on your function.php file.
3) Go in a shipping zone settings and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done and you can test it.