WooCommerce:根据国家/地区禁用多种付款方式

WooCommerce: Disable multiple Payment methods based on country

我在这里想做的是根据客户国家/地区隐藏付款方式。

我一直在尝试这样做,但没有成功

代码如下:

add_filter( 'woocommerce_available_payment_gateways', 'custom_payment_gateway_disable_country' );
  
function custom_payment_gateway_disable_country( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( isset( $available_gateways['bacs' && 'cheque'] ) && WC()->customer->get_billing_country() <> 'US' ) {
        unset( $available_gateways['bacs' && 'cheque'] );
    } else {
        if ( isset( $available_gateways['cod'] ) && WC()->customer->get_billing_country() == 'US' ) {
            unset( $available_gateways['cod'] );
        }
    }
    return $available_gateways;
}

有人可以把我推向正确的方向吗?任何帮助将不胜感激!

您的 if/else 陈述是错误的。因为 isset( $available_gateways['bacs' && 'cheque'] 永远不会工作。

参见:PHP If Statement with Multiple Conditions

所以你得到:

function filter_woocommerce_available_payment_gateways( $payment_gateways ) {
    // Not on admin
    if ( is_admin() ) return $payment_gateways;

    // Get country
    $customer_country = WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->customer->get_billing_country();
    
    // Country = US
    if ( in_array( $customer_country, array( 'US' ) ) ) {       
        // Cod
        if ( isset( $payment_gateways['cod'] ) ) {
            unset( $payment_gateways['cod'] );
        }
    } else {
        // Bacs & Cheque
        if ( isset( $payment_gateways['bacs'] ) && isset( $payment_gateways['cheque'] ) ) {
            unset( $payment_gateways['bacs'] );
            unset( $payment_gateways['cheque'] );
        }       
    }
    
    return $payment_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );