Menu Close

WooCommerce: Disable Payment Method If Product Category @ Cart

default

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 CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_unset_gateway_by_category' );
 
function bbloomer_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;
}

Mini-Plugin: Business Bloomer WooCommerce Toggle Payments By Category

You don’t feel confident with coding? You need more control over your payment/category exclusions? You don’t want to purchase yet another bloated, expensive plugin? Great!

Business Bloomer WooCommerce Toggle Payments By Category is a mini WooCommerce plugin, without the usual hassles. One feature. Lifetime license. No annoying subscriptions. 1 plugin file. A few lines of code. No banners. No up-sells. No WP notifications. Use it on as many websites as you like. Lifetime support. 1-page documentation. A single and easy admin dashboard.

Screenshot of the settings? Here you go:

Quick demo? Here it is:

As you can see the settings are pretty straight forward. Select a payment method you wish to hide/show from the left, and the category that should trigger that from the right. Add more rules if needed. Simple!

Advanced Plugin: WooCommerce Conditional Payment Gateways

If you require something more advanced, I decided to look for a reliable plugin that achieves the same result of this snippet (and much 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, categories, tags, 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 🙂

View Source
Posted in WooCommerce Tips