Payments: Only Display Supported Payment Gateways at Checkout

This code snippet will filter the payment gateways offered at checkout to only enable the display of payment methods supported by Autoship Cloud when the customer has 1 or more products selected for Autoship in their cart.

Important: Always test customizations to your site on a staging environment before making changes to your live / production site.  If you are not experienced with testing changes on a staging site, here is a good article to review.

/**
* Filter for only Valid Autoship Payment Gateways
* @param array $gateways The array of available payment gateways.
* @return array The filtered $gateways
*/
function xx_filter_autoship_gateways( $gateways ){
    // if Autoship is not installed return all gateways.
    if ( !function_exists( 'autoship_cart_has_valid_autoship_items' ) )
    return $gateways;
    // Check if Cart has autoship products.
    if( !autoship_cart_has_valid_autoship_items() )
    return $gateways;
    /**
    * Get Array of the Valid Autoship Gateways
    * Supported Payment Gateways include:
    * 'authorize_net_cim_credit_card'       => 'authorize_net',
    * 'stripe'                              => 'stripe',
    * 'trustcommerce'                       => 'trustcommerce',
    * 'autoship-test-gateway'               => 'test_gateway',
    * 'ppec_paypal'                         => 'ppec',
    * 'braintree_credit_card'               => 'braintree',
    * 'braintree_paypal'                    => 'braintree_paypal',
    * 'cybersource'                         => 'cybersource',
    * 'nmi_gateway_woocommerce_credit_card' => 'nmi',
    * 'sagepaymentsusaapi'                  => 'sagepaymentsusaapi',
    */
    $valid_gateways = array( 'ppec_paypal' => true );
    //filter out any gateways that don't work.
    foreach ($gateways as $key => $gateway) {
      if ( !isset( $valid_gateways[$key] ) )
      unset( $gateways[$key] );
    }
    return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'xx_filter_autoship_gateways', 1);

Still need help? Contact Us Contact Us