Payment Gateways: Conditionally Enable Supported Payment Gateways Only When Autoship Products Are In Cart

If you are using any payment gateways for WooCommerce that are not supported by Autoship Cloud powered by QPilot, you can add the following snippet to your functions.php file to conditionally filter and only display the Payment Gateways that are supported by Autoship Cloud when 1 or more products selected for Autoship are added to the 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.

Example

/**
* 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