2

I know WooCommerce is not well-received here, because it's not directly related to WordPress, but as support on the WooCommerce forums is just that terrible and people here having proven they're capable and knowledgeable, I'll give it a try.

A client of ours wants to show/hide specific payment gateways for specific user groups. Here's the situation:

  • User Group A: Customer
  • User Group B: Company

Only B is supposed to be able to pay via invoice, so after they receive the goods.

Here's where I'm at so far:

add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );

 function payment_gateway_disable_private( $available_gateways ) {
   if ( isset( $available_gateways['authorize'] ) && $woocommerce->customer->get_role($role) <> 'Company' ) {
      unset( $available_gateways['authorize'] );
   } else if ( isset( $available_gateways['cheque'] ) && $woocommerce->customer->get_role($role) == 'Customer' ) {
      unset( $available_gateways['cheque'] );
}
   return $available_gateways;
}

"cheque" is the name of the payment gateway used for invoice payment.

It's not working yet, so what am I missing? Any help is greatly appreciated!

closed as off-topic by Pieter Goosen, Brad Dalton, Howdy_McGee, Mark Kaplun, Robert hue Aug 1 '15 at 17:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

3

Arrrrg a WooCommerce question... quick burn him at the stake!

There's a couple of problems with your callback function above:

  • you are not declaring global $woocommerce so you do not have access to the $woocommerce global variable.
  • and $woocommerce->customer holds the state of the WC_Customer class however this class does not contain a get_role() method, further more you are not defining anywhere in your callback the value of the $role variable so that wouldn't work anyway even if the method existed.

Instead, check for the user role in the roles array returned on the WP_User object:

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['authorize'] ) && in_array('company', $user->roles) ) {
        unset( $available_gateways['authorize'] );
    } else if ( isset( $available_gateways['cheque'] ) && in_array('customer', $user->roles)  ) {
        unset( $available_gateways['cheque'] );
    }

   return $available_gateways;

}

Note: if the user is logged out, they will have no role, so you may wish to account for that and do something different such as redirect the user to login first before shopping depending on your use case, you can use is_user_logged_in() for that.

  • I had those declared before when I played around a bit, but I removed them again as none of my modifications seemed to work. – theHubi Jul 29 '15 at 11:00
  • Thanks for the clarification. I kinda figured out I had to use WordPress functions for that as I read already that customer does not have a get_role, however I'm not that accustomed to all the variables and hooks of WordPress yet. I'll see what else I need and work from there. Thanks a lot! – theHubi Jul 29 '15 at 11:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.