Search code examples
wordpresswoocommercesquare

Trying to Conditionally Hide Square in WooCommerce


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?


Solution

  • 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;
    }