如果您想要為特定國家/地區僅顯示一種運送方式,則需要一些額外的編碼。在此示例中,我們將禁用所有運送方式,但 “免費送貨” 。這裡是添加到您的 functions.php 的 PHP 代碼
代碼段#1:在 1 個運送區域中取消設置 1 個免費版本
/**
* @snippet Hide one shipping option in one zone when Free Shipping is available
* @sourcecode https://businessbloomer.com/?p=260
* @author Rodolfo Melogli
* @compatible WooCommerce 3.1.1
*/
add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available_in_zone', 10, 2 );
function bbloomer_unset_shipping_when_free_is_available_in_zone( $rates, $package ) {
// Only unset rates if free_shipping is available
if ( isset( $rates['free_shipping:8'] ) ) {
unset( $rates['flat_rate:1'] );
}
return $rates;
}
代碼段#1:在可用空閑時,在所有區域中取消設置所有費率
/**
* @snippet Hide ALL shipping rates in ALL zones when Free Shipping is available
* @sourcecode https://businessbloomer.com/?p=260
* @author Rodolfo Melogli
* @compatible WooCommerce 3.1.1
*/
add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available_all_zones', 10, 2 );
function bbloomer_unset_shipping_when_free_is_available_all_zones( $rates, $package ) {
$all_free_rates = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$all_free_rates[ $rate_id ] = $rate;
break;
}
}
if ( empty( $all_free_rates )) {
return $rates;
} else {
return $all_free_rates;
}
}
對於舊版本的 WooCommerce [2.1-2.5](當 Free 可用時,取消設置一個速率)
/**
* @snippet Hide one shipping option when Free Shipping is available
* @sourcecode https://businessbloomer.com/?p=260
* @author Rodolfo Melogli
* @compatible WooCommerce 2.4.10
*/
add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available', 10, 2 );
function bbloomer_unset_shipping_when_free_is_available( $rates, $package ) {
// Only unset rates if free_shipping is available
if ( isset( $rates['free_shipping'] ) ) {
unset( $rates['flat_rate'] );
return $rates;
}
}
對於舊版本的 WooCommerce [2.1-2.5](免費提供時,設置所有費率)
/**
* @snippet Hide ALL shipping options when Free Shipping is available
* @sourcecode https://businessbloomer.com/?p=260
* @author Rodolfo Melogli
* @compatible WooCommerce 2.4.10
*/
add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available', 10, 2 );
function bbloomer_unset_shipping_when_free_is_available( $rates, $package ) {
// Only unset rates if free_shipping is available
if ( isset( $rates['free_shipping'] ) ) {
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
return $rates;
}
達到 WooCommerce 2.1
// Hide standard shipping option when free shipping is available
add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );
/**
* Hide Standard Shipping option when free shipping is available
*
* @param array $available_methods
*/
function hide_standard_shipping_when_free_is_available( $available_methods ) {
if( isset( $available_methods['free_shipping'] ) AND isset( $available_methods['flat_rate'] ) ) {
// remove standard shipping option
unset( $available_methods['flat_rate'] );
}
return $available_methods;
}
如何添加此代碼?
1 、您可以將 PHP 代碼片段放置在主題或子主題的 functions.php 文件的底部(如果是 CSS 代碼,請添加到子主題的 style.css 文件底部)修改之前建議先備份原始文件,若出現錯誤請先刪除此代碼。
2 、 WordPress 4.9 後改進了主題編輯器,對於 CSS 代碼也可打開網站前台編輯器的【自定義】,複製代碼添加到自定義 css 中。
此代碼是否可用?
如需幫助或是您有更好的方案想分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 用戶也希望您的參與。