Home » WooCommerce: Disable Payment Method If Product Category @ Cart

WooCommerce: Disable Payment Method If Product Category @ Cart

by Tutor Aspire

Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart.

There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the Cart and (2) disable a specific payment ID if the target product category is in the Cart.

May sound difficult but don’t worry – solution is right below. Enjoy!

WooCommerce: Disable Payment Method for Specific Category
WooCommerce: Disable Payment Method for Specific Category. In this example, let’s imagine that “Sale Product” belongs to category “SALE”; well, if “Sale” product category is in the Cart, I wish to hide “COD” (cash on delivery) at Checkout. Makes sense!

PHP Snippet: Disable Payment Method for Specific Category @ WooCommerce Checkout

/**
 * @snippet       Disable Payment Method for Specific Category
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://tutoraspire.com
 */
 
add_filter( 'woocommerce_available_payment_gateways', 'tutoraspire_unset_gateway_by_category' );
 
function tutoraspire_unset_gateway_by_category( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( ! is_checkout() ) return $available_gateways;
    $unset = false;
    $category_id = 8; // TARGET CATEGORY
    foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
        $terms = get_the_terms( $values['product_id'], 'product_cat' );    
        foreach ( $terms as $term ) {        
            if ( $term->term_id == $category_id ) {
                $unset = true; // CATEGORY IS IN THE CART
                break;
            }
        }
    }
    if ( $unset == true ) unset( $available_gateways['cod'] ); // DISABLE COD IF CATEGORY IS IN THE CART
    return $available_gateways;
}

Business Bloomer Premium Snippet®: WooCommerce Disable Payments By Category

You don’t feel confident with coding? Check.

You need more control over multiple payment/category exclusions? Check.

You don’t want to purchase yet another bloated, expensive plugin? Check.

Business Bloomer’s WooCommerce Disable Payments By Category Premium Snippet® is a mini WooCommerce plugin, without the usual hassles. Lifetime license. Just a few lines of code. Use it on as many websites as you like. Get lifetime support and documentation. A single and super-easy admin dashboard. Download as a plugin, as a Code Snippet or as raw PHP code.

Screenshot? Here you go:

Quick demo? Here it is:

As you can see Business Bloomer’s WooCommerce Disable Payments By Category Premium Snippet® settings are pretty straight forward. Select a payment method you wish to hide from the left, and the category that should trigger that from the right. Add more rules if needed. Simple!

Interested?


WooCommerce Disable Payments By Category – Premium Snippet®

Lifetime license, lifetime support, lifetime updates

Download as plugin, Code Snippet or raw code

Use on unlimited websites

30 days 100% money-back guarantee

FAQ & Documentation

What does this Premium Snippet® do? Where do I find the settings?

It adds a new table under WooCommerce > Settings > Payments where you can define which payment method gets disabled at Checkout if a certain product category is in the Cart.

What’s the difference between the plugin, Code Snippet and raw code versions?

There is no difference, code is the exact same. And you will get access to all of them, so that you can pick whatever version suits you best. Want to use it as a plugin? Install it as a normal plugin via the supplied ZIP. Prefer using Code Snippets plugin? Cool, simply import the provided JSON snippet! Familiar with code? Great, easily place the raw code into your child theme’s functions.php file. Just pick one 🙂

Where can I get lifetime support?

On this same page. Simply click here to scroll down to the comments area. If you use the same email under your customer account, your comment will receive priority.

Where can I download my files?

Just head over to the My Account > My Downloads page. Here’s a direct link

What if a new version is released? How will I know?

As long as you don’t unsubscribe from Business Bloomer email list, you will receive an email once a new update is available. You can then go to the Downloads page and find the latest version.

I’ve tried the snippet but it doesn’t suit my needs. Can I get a refund?

Sure, and you get 100% money back if you request a refund within 30 days from your purchase. As of now there is no automated system on the My Account > My Orders page, so drop me an email and I’ll process your refund. Working on it though (expect an article on BB soon!)

Which payment gateways is this compatible with?

ALL payment gateways that show in the default Order Review area @ WooCommerce checkout (so PayPal, Stripe, etc.). Also, since version 1.1, WooCommerce Disable Payments By Category is also compatible with:
PeachPay (hides express button on single, Cart, Checkout)
AfterPay (hides buttons on single, archive, Cart, Checkout)
Stripe GooglePay & ApplePay (hides express buttons on single, Cart, Checkout)
PayPal Express by WebToffee (hides express button on Cart, Checkout)

I have a pre-sale / post-sale question?

Cool! Post your request now and I’ll do my best to get back to you asap

Is There a More Advanced WooCommerce “Payment Gateways by Category” Plugin?

If you require something more advanced, I decided to look for a reliable plugin that achieves the same result of this snippet (and more).

In this case, I found the WooCommerce Conditional Payment Gateways plugin to be the most complete when you need to enable/disable payment gateways based on certain criteria. You can create unlimited “rules” and use, for example, cart totals, billing country, shipping country, user role and much more to define which payment gateway shows and which not.

But in case you don’t want to use plugins and wish to code (or wish to try that), then keep reading 🙂

You may also like