How to Disable a Payment Method in Checkout for Certain Products

Sometimes, you don’t want to offer a particular payment gateway in your WooCommerce checkout if a customer has a certain product in their cart.

Here’s a code snippet that you can add to your child theme’s functions.php file to help:

/*
 * Disable PayPal payment method in the checkout if certain 
 * products are present in the cart.
 *
 * Add this to your theme's functions.php file
 */
 
add_filter( 'woocommerce_available_payment_gateways', 'filter_gateways', 1);
function filter_gateways( $gateways ){
  global $woocommerce;
  foreach ($woocommerce->cart->cart_contents as $key => $values ) {
  
    // store product IDs in array PayPal method is disabled at checkout
    $nonPPproducts = array(1111,2222,3333);		// LIST YOUR PRODUCTS HERE

    if ( in_array( $values['product_id'], $nonPPproducts ) ) {	
      unset($gateways['paypal']);
      // You can unset other gateways using the gateway ID e.g. "cod", "bacs", "stripe"
      break;
    }
  }
  
  return $gateways;
  
}

You simply have to add the product(s) by their ID to the array list. If you want to disable other payment methods, you can find their Gateway ID under WooCommerce > Settings > Checkout:

Payment Gateway ID WooCommerce

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s