問題描述
在 WooCommerce 3 中,我有以下運輸選項(設置):
In WooCommerce 3, I have these shipping options (settings):
- 免運費:
free_shipping:1
- 最低訂單金額設為$50
. - 正常運輸
flat_rate:3
- 金額$5
. - 快遞
flat_rate:5
- 金額$10
.
- Free Shipping:
free_shipping:1
- Minimum order amount is set at$50
. - Normal Shipping
flat_rate:3
- Amount$5
. - Express Shipping
flat_rate:5
- Amount$10
.
我希望 快遞 選項始終可用(如圖所示).
I would like Express Shipping option to be always available (shown).
但是當免費送貨可用時(意味著客戶在購物車中的金額超過 50 美元),我只想隱藏正常送貨.
But when Free shipping is available (meaning that the customer has more than $50 in the cart) I would like to hide Normal Shipping only.
因此,當免費送貨
不可用(和隱藏)時,可用的運費將為正常送貨 和快遞.
So when Free shipping
is NOT available (and hidden), the available shipping rates will be Normal Shipping and Express Shipping.
這可能嗎?我怎樣才能在 WooCommerce 3 上獲得這個?
Is that possible? How can I get this on WooCommerce 3?
推薦答案
基于 WooCommerce 官方代碼段,做了一些小改動,當免費送貨可用時,您將只能隱藏您的第一個統一費率:
Based on the official WooCommerce snippet code, making some light changes, you will be able to hide only your first flat rate when free shippings is available:
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 上測試并有效.
Tested on WooCommerce 3 and works.
刷新運輸緩存:
1) 首先清空您的購物車.
2) 此代碼已保存在您的 function.php 文件中.
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.
這篇關于在 WooCommerce 3 中提供免費送貨時隱藏特定的統一費率的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!