Php 根据商业中的特定城市显示货到付款(COD)

Php 根据商业中的特定城市显示货到付款(COD),php,wordpress,woocommerce,payment-gateway,checkout,Php,Wordpress,Woocommerce,Payment Gateway,Checkout,在Woocommerce中,我仅使用以下代码显示特定城市的货到付款方式: function payment_gateway_disable_city( $available_gateways ) { global $woocommerce; if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_shipping_city() == 'New York') {

在Woocommerce中,我仅使用以下代码显示特定城市的货到付款方式:

function payment_gateway_disable_city( $available_gateways ) {
    global $woocommerce;

    if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_shipping_city() == 'New York') {
        unset( $available_gateways['cod'] );
    } 
    return $available_gateways;
}

add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_city' );
它很好用。现在我需要处理多个城市,如华盛顿,旧金山等… 因此,我尝试了以下方法:

function payment_gateway_disable_city( $available_gateways ) {
    global $woocommerce;

    if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_shipping_city() == 'New York', 'San Fransisco' ) {
        unset( $available_gateways['cod'] );
    } 
    return $available_gateways;
}

add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_city' );
但它不起作用……我看到“WordPress遇到技术难题”


感谢您的帮助。

已更新

以下代码将启用特定定义城市的COD付款

您需要使用一组允许的城市,例如:


代码进入活动子主题(或活动主题)的functions.php文件。已测试并运行。

谢谢,我只是想知道,在我集成此代码后,只有我提到的城市才会有COD!如果选择了其他城市,客户将无法选择COD,对吗?@Anas我对代码做了一些更改。是的,您将有COD支付仅启用纽约和旧金山…城市在函数中定义为一个数组,您可以添加任意数量的城市。这就是我一直在寻找的,非常感谢,它可以工作。
add_filter( 'woocommerce_available_payment_gateways', 'cities_based_payment_gateway_cod' );
function cities_based_payment_gateway_cod( $available_gateways ) {
    if ( is_admin() ) return $available_gateways; // Only on frontend

    // HERE define the allowed cities in this array 
    $cities = array( 'New York', 'San Francisco' );

    if ( isset( $available_gateways['cod'] ) && ! in_array( WC()->customer->get_shipping_city(), $cities ) ) {
        unset( $available_gateways['cod'] );
    } 
    return $available_gateways;
}