【问题标题】:How to Hide Payment Method in Woocommerce based on Postal Code如何在基于邮政编码的 Woocommerce 中隐藏付款方式
【发布时间】:2013-06-05 14:16:30
【问题描述】:

在这个 woocommerce 设置中,我有 2 种付款方式,Paypal货到付款

现在如何仅针对某些邮政编码隐藏/禁用货到付款

这是我在 Gist 上找到的代码

//  Disable gateway based on country
function payment_gateway_disable_country( $available_gateways ) {
    global $woocommerce;
    if ( isset( $available_gateways['ccavenue'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
        unset(  $available_gateways['ccavenue'] );
    } else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_country() == 'IN' ) {
        unset( $available_gateways['paypal'] );
    }
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );

Gist Link

【问题讨论】:

标签: wordpress woocommerce


【解决方案1】:

要禁用/隐藏“货到付款”,请将此代码放在主题的 function.php 中。

更多详情:woocommerce-hide-payment-gatway-based-on-visitors-country

//  Disable gateway based on country
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
    unset(  $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );

【讨论】:

【解决方案2】:

在“结帐页面”中,用户可以有两个地址 - 账单地址和送货地址。

只有在装满的情况下才能正常工作,我更改了一些代码。如果设置了运输国家代码,您必须测试它,如果不只是用户国家代码:

function payment_gateway_disable_country( $available_gateways ) {
    global $woocommerce;
    $country = !empty($woocommerce->customer->get_shipping_country()) ? $woocommerce->customer->get_shipping_country() : $woocommerce->customer->get_country();
    if ( isset( $available_gateways['cod'] ) && $country <> 'CZ' ) {
        unset(  $available_gateways['cod'] );
    }
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );

【讨论】:

    【解决方案3】:

    在上面的代码中,您使用国家代码禁用支付网关,但您提到您希望使用邮政编码。

    使用woocommerce_available_payment_gateways 是对的,但您必须使用WC()-&gt;customer-&gt;get_shipping_postcode()(或在某些情况下使用WC()-&gt;customer-&gt;get_billing_postcode())而不是使用$woocommerce-&gt;customer-&gt;get_country()

    您提到了 PayPal 和货到付款支付网关,我们需要它们的 ID,相应的有 paypalcod

    在下面的代码中,让我们为几个邮政编码停用货到付款,例如“1234”和“5678”:

    add_filter( 'woocommerce_available_payment_gateways', function( $available_gateways ) {
        
        // if Cash on Delivery is already disabled, let's exit the function
        if( empty( $available_gateways['cod'] ) ) {
            return $available_gateways['cod'];
        }
    
        // get postal code
        $postal_code = WC()->customer->get_billing_postcode();
    
        // deactivate payment method
        if( in_array( $postal_code, array( '1234', '5678' ) ) ) {
            unset(  $available_gateways['cod'] );
        }
            
        return $available_gateways;
    
    } );
    

    可以将代码插入到您当前的主题functions.php 文件或自定义插件中。您可以在本教程中找到更多信息:https://rudrastyh.com/woocommerce/hide-payment-methods-based-on-postal-code.html

    【讨论】:

    • 当链接到您自己的网站或内容(或您附属的内容)时,您must disclose your affiliation in the answer 以免被视为垃圾邮件。根据 Stack Exchange 政策,在您的用户名中包含与 URL 相同的文本或在您的个人资料中提及它不被视为充分披露。
    猜你喜欢
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2016-04-13
    • 2021-02-21
    相关资源
    最近更新 更多