Php 当Woocommerce中只有一个区域提供免费配送时,隐藏其他配送选项

Php 当Woocommerce中只有一个区域提供免费配送时,隐藏其他配送选项,php,wordpress,woocommerce,cart,shipping-method,Php,Wordpress,Woocommerce,Cart,Shipping Method,我需要一种方法来实现以下目标:如果可以免费发货,并且订单正在发货到特定区域,则隐藏所有其他发货方法 我发现了这个片段: function hide_shipping_when_free_is_available( $rates ) { $free = array(); foreach ( $rates as $rate_id => $rate ) { if ( 'free_shipping' === $rate->method_id ) {

我需要一种方法来实现以下目标:如果可以免费发货,并且订单正在发货到特定区域,则隐藏所有其他发货方法

我发现了这个片段:

function hide_shipping_when_free_is_available( $rates ) {
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 100 );

如何向其中添加条件以仅将其应用于前往一个区域的订单?

以下代码将在特定区域提供免费配送时隐藏所有其他配送方法(您将在函数中定义目标区域ID或区域名称):

代码进入活动子主题(或活动主题)的function.php文件。测试和工作

/**
 * Hide shipping rates when free shipping is available.
 * Updated to support WooCommerce 2.6 Shipping Zones.
 *
 * @param array $rates Array of rates found for the package.
 * @return array
 */
function my_hide_shipping_when_free_is_available( $rates ) {
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );
/**
 * Hide shipping rates when free shipping is available.
 * Updated to support WooCommerce 2.6 Shipping Zones.
 *
 * @param array $rates Array of rates found for the package.
 * @return array
 */
function my_hide_shipping_when_free_is_available( $rates ) {
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );