0

I have a client who only wants to show the Square payment method in WooCommerce to shop managers and admins. There are other payment methods they want to similarly hide. For example, bacs, cod and cheque. The following code works for anything except Square:

add_filter( 'woocommerce_available_payment_gateways', 'client_pmts_enable_manager' );
  
function client_pmts_enable_manager( $available_gateways ) {
   if ( isset( $available_gateways['bacs'] ) && ! current_user_can( 'manage_woocommerce' ) ) {
      unset( $available_gateways['bacs'] );
   } 
   if ( isset( $available_gateways['cod'] ) && ! current_user_can( 'manage_woocommerce' ) ) {
      unset( $available_gateways['cod'] );
   } 
   if ( isset( $available_gateways['cheque'] ) && ! current_user_can( 'manage_woocommerce' ) ) {
      unset( $available_gateways['cheque'] );
   } 
   if ( isset( $available_gateways['square'] ) && ! current_user_can( 'manage_woocommerce' ) ) {
      unset( $available_gateways['square'] );
   } 
   return $available_gateways;
}

Can anyone help me discover the name of the Square gateway so that I can unset it for non-admin/shop managers?

Christopher
  • 5
  • 1
  • 2
  • Go to Woocommerce -> Settings -> Payments . Then edit the payment you want and check the url - https://prnt.sc/1s624eu . This is the gateway name of the payment method. – Snuffy Sep 14 '21 at 13:10
  • See: [How to debug in WooCommerce 3](https://stackoverflow.com/questions/61740111/how-to-debug-in-woocommerce-3) – 7uc1f3r Sep 14 '21 at 13:42
  • First of all, you should activate "Square payment method" and then need to print all payment gateways like this echo "
    "; print_r( $available_gateways); exit; then you will get all payment methods
    – Rajeev Singh Sep 21 '21 at 05:20

1 Answers1

0

Try with the following, I think the ID is in fact "square_credit_card":

add_filter( 'woocommerce_available_payment_gateways', 'client_pmts_enable_manager' );
  
function client_pmts_enable_manager( $available_gateways ) {
   if ( isset( $available_gateways['bacs'] ) && ! current_user_can( 'manage_woocommerce' ) ) {
      unset( $available_gateways['bacs'] );
   } 
   if ( isset( $available_gateways['cod'] ) && ! current_user_can( 'manage_woocommerce' ) ) {
      unset( $available_gateways['cod'] );
   } 
   if ( isset( $available_gateways['cheque'] ) && ! current_user_can( 'manage_woocommerce' ) ) {
      unset( $available_gateways['cheque'] );
   } 
   if ( isset( $available_gateways['square_credit_card'] ) && ! current_user_can( 'manage_woocommerce' ) ) {
      unset( $available_gateways['square_credit_card'] );
   } 
   return $available_gateways;
}
businessbloomer
  • 1,115
  • 7
  • 11