Cart / Checkout: Conditionally Enable Autoship Gateway for Autoship Cart Items

If you are using a payment gateway not supported currently by an Autoship Cloud integration, you can add the following snippet to your functions.php file to conditionally enable the Autoship supported gateway, only when there are items selected for Autoship are present in 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