为访客和特定用户角色禁用 WooCommerce 支付网关 [英] Disable WooCommerce payment gateway for guests and specific user roles

查看:66
本文介绍了为访客和特定用户角色禁用 WooCommerce 支付网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已为我网站中的一个用户角色(客户")禁用了发票付款方式,但现在我需要向此规则添加另一个用户角色(企业"),但我不知道如何让它起作用.当我添加第二个角色时,代码完全停止工作,最终向所有用户显示网关.

I have disabled the invoice payment mehtod for one of the user roles in my site ('customer') but now I need to add another user role ('business') to this rule and I can't figure out how to make it work. When I add the second role, the code stops working altogether and it ends up showing the gateway to all users.

这是我用来禁用网关的代码:

Here's the code I'm using to disable the gateway:

我对 PHP 不是很有经验,所以任何帮助都将不胜感激.如果您有机会更正我的代码以适应用例,我将不胜感激.

I'm not very experienced with PHP so any help will be tremendously appreciated. If there's any chance you can correct my code to fit the use case, I would be very grateful.

add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );

 function payment_gateway_disable_private( $available_gateways ) {

    $user = wp_get_current_user();

    if ( isset( $available_gateways['igfw_invoice_gateway'] ) && !is_user_logged_in() || isset( $available_gateways['igfw_invoice_gateway'] ) && in_array('customer', $user->roles)  ) {
        unset( $available_gateways['igfw_invoice_gateway'] );
    }

   return $available_gateways;

}

想法?

推荐答案

你的 if 语句有错误 (你也可以使用 current_user_can() 函数作为用户角色) 喜欢:

There is a mistake in your if statement (also you can use current_user_can() function for user roles) like:

add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
    if ( ( ! is_user_logged_in() || current_user_can('customer') || current_user_can('business') ) 
    && isset( $available_gateways['igfw_invoice_gateway'] ) ) {
        unset( $available_gateways['igfw_invoice_gateway'] );
    }
   return $available_gateways;
}

或使用 global $current_user;array_intersect() 函数:

or with the global $current_user; and array_intersect() function:

add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
    global $current_user;

    // Here define your user roles
    $user_roles = array( 'customer', 'business' );

    if ( ( ! is_user_logged_in() || array_intersect( $current_user->roles, $user_roles ) ) 
    && isset( $available_gateways['igfw_invoice_gateway'] ) ) {
        unset( $available_gateways['igfw_invoice_gateway'] );
    }
   return $available_gateways;
}

现在应该更好用了.

这篇关于为访客和特定用户角色禁用 WooCommerce 支付网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆