在 WooCommerce 3 中提供免费送货时隐藏特定的统一费率

Lex*_*ing 2 php wordpress shipping cart woocommerce

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

  1. 免费送货:free_shipping:1- 最低订单金额设置为$50
  2. 正常运输flat_rate:3- 金额$5
  3. 快递运输flat_rate:5- 金额$10

我希望快递运输选项始终可用(如图所示)。

但是当免费送货可用时(意味着客户在购物车中的金额超过 50 美元),我只想隐藏 正常送货

所以,当Free shipping提供(或隐藏),可用的运费将是正常的运输和快递运输。

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

Loi*_*tec 5

根据官方的 WooCommerce 代码段,进行一些细微的更改,当免费送货可用时,您将能够仅隐藏您的第一个统一费率:

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;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

在 WooCommerce 3 上测试并有效。

刷新运输缓存:
1) 首先清空您的购物车。
2) 此代码已保存在您的 function.php 文件中。
3) 进入运输区域设置并禁用一个“统一费率” (例如)和“保存”。然后重新启用“统一费率”和“保存”。你已经完成了,你可以测试它。