0

I created this code on WooCommerce to disable multiple payment gateways (cardgatecreditcard, cardgategiropay, cardgateideal and cardgatesofortbanking) for two different shipping methods (request_shipping_quote and flat_rate). But how do I simplify it?

// Disable Payment Gateway For Specific Shipping Method

add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_326' );
  
function bbloomer_gateway_disable_shipping_326( $available_gateways ) {
     
   if ( ! is_admin() ) {
        
      $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
        
      $chosen_shipping = $chosen_methods[0];
        
      if ( isset( $available_gateways['cardgatecreditcard'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) {
         unset( $available_gateways['cardgatecreditcard'] );
      }
     if ( isset( $available_gateways['cardgategiropay'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) {
         unset( $available_gateways['cardgategiropay'] );
      }
     if ( isset( $available_gateways['cardgateideal'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) {
         unset( $available_gateways['cardgateideal'] );
      }
     if ( isset( $available_gateways['cardgatesofortbanking'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) {
         unset( $available_gateways['cardgatesofortbanking'] );
      }
     if ( isset( $available_gateways['cardgatecreditcard'] ) && 0 === strpos( $chosen_shipping, 'request_shipping_quote' ) ) {
         unset( $available_gateways['cardgatecreditcard'] );
      }
     if ( isset( $available_gateways['cardgategiropay'] ) && 0 === strpos( $chosen_shipping, 'request_shipping_quote' ) ) {
         unset( $available_gateways['cardgategiropay'] );
      }
     if ( isset( $available_gateways['cardgateideal'] ) && 0 === strpos( $chosen_shipping, 'request_shipping_quote' ) ) {
         unset( $available_gateways['cardgateideal'] );
      }
     if ( isset( $available_gateways['cardgatesofortbanking'] ) && 0 === strpos( $chosen_shipping, 'request_shipping_quote' ) ) {
         unset( $available_gateways['cardgatesofortbanking'] );
      }
   }
     
   return $available_gateways;
     
}

1 Answers1

0

Try the following:

add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
function filter_woocommerce_available_payment_gateways( $available_gateways ) { 

    $gateways_to_disable = array( 'cardgatecreditcard', 'cardgategiropay', 'cardgateideal', 'cardgatesofortbanking' );
    $shipping_methods = array( 'flat_rate', 'request_shipping_quote' );
    $disable_gateways = false;

    // Check if we need to disable gateways
    foreach ( $shipping_methods as $shipping_method ) {
        if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
    }
    
    // If so, disable the gateways
    if ( $disable_gateways ) {
        foreach ( $available_gateways as $id => $gateway ) {
            if ( in_array( $id, $gateways_to_disable ) ) {
                unset( $available_gateways[$id] );
            }
        }
    }
    return $available_gateways;
}
Terminator-Barbapapa
  • 3,063
  • 2
  • 5
  • 16