WooCommerce: Enable or Disable Payment Methods Based on Product Categories

Offering every payment method for every product may not always make sense if you run a WooCommerce store. For example, you might want to restrict bank transfers to specific product categories, such as bulk or wholesale items, or disable Cash on Delivery (COD) for digital downloads.

This article will show you how to enable or disable payment methods in WooCommerce based on the product categories in your cart. This type of setup is extremely helpful in simplifying your checkout process and providing your customers with only the payment options that are appropriate for their purchase.

Fast And Secure Hosting For WordPress

Experience lightning-fast speed, rock-solid security, and world-class support tailored for WordPress. Simply better hosting.

Why Restrict Payment Methods by Category?

There are several real-world reasons for store owners to restrict payment methods:

  • Digital products don’t need COD, so you might want to force online payment.
  • Bulky or high-value items may require bank transfers instead of instant payments.
  • Some shipping partners or fulfilment methods only support specific payment types.

If you’ve ever run into a situation where a customer picks COD for a product that can’t be shipped that way you know it’s time to add some logic to your checkout flow.

Also Read: Extra Fees or Additional Charges in WooCommerce

Enable or Disable Payment Methods

The Solution: Filter Available Payment Gateways

The filter “woocommerce_available_payment_gateways” allows you to manage which payment methods display at checkout.

Copy and paste the below code snippet to Your Theme’s functions.php File.

In this code, disable Cash on Delivery if the cart contains products from specific categories:

add_filter('woocommerce_available_payment_gateways', 'woobooster_filter_gateways_based_on_category');

function woobooster_filter_gateways_based_on_category($available_gateways) {
    if (is_admin()) return $available_gateways;

    // Define the category slug(s)
    $restricted_categories = array('gadgets'); // Change to your category slugs
    $found_in_cart = false;

    // Loop through cart items
    foreach (WC()->cart->get_cart_contents() as $cart_item) {
        $product_id = $cart_item['product_id'];
        $product_categories = wc_get_product_term_ids($product_id, 'product_cat');

        foreach ($restricted_categories as $category_slug) {
            $category = get_term_by('slug', $category_slug, 'product_cat');
            if ($category && in_array($category->term_id, $product_categories)) {
                $found_in_cart = true;
                break 2;
            }
        }
    }

    // If restricted category found, disable COD
    if ($found_in_cart && isset($available_gateways['cod'])) {
        unset($available_gateways['cod']);
    }

    return $available_gateways;
}

Suggested Read: Partial COD (Cash on Delivery) in WooCommerce

Reverse Scenario: Only Allow a Gateway for a Specific Category

If you want to enable a gateway only when specific categories are in the cart, reverse the logic. Here’s an example of enabling “bacs” (bank transfer) only when products from the “bulk-orders” category are in the cart.

add_filter('woocommerce_available_payment_gateways', 'woobooster_enable_bacs_for_bulk_orders');

function woobooster_enable_bacs_for_bulk_orders($available_gateways) {
    if (is_admin()) return $available_gateways;

    $allowed_category = 'bulk-orders';
    $found = false;

    foreach (WC()->cart->get_cart_contents() as $cart_item) {
        $product_id = $cart_item['product_id'];
        $categories = wc_get_product_term_ids($product_id, 'product_cat');

        $term = get_term_by('slug', $allowed_category, 'product_cat');
        if ($term && in_array($term->term_id, $categories)) {
            $found = true;
            break;
        }
    }

    // Disable all other methods except BACS if the category is found
    if ($found) {
        foreach ($available_gateways as $key => $gateway) {
            if ($key !== 'bacs') {
                unset($available_gateways[$key]);
            }
        }
    }

    return $available_gateways;
}
Looking for a WordPress Developer?

Are you in need of a skilled WordPress developer to bring your website vision to life?
Look no further! Whether you need custom themes, plugin development, site optimization, or ongoing support, I offer expert WordPress development services to suit your needs.

Conclusion

Customizing WooCommerce payment options based on cart content is an excellent way to enhance the shopping experience and decrease failed orders. Whether you want to disable COD for digital goods or limit PayPal to certain shipping zones, this type of logic gives you more control.

Remember to always test changes in a staging environment before deploying them to your live store. If you’re not comfortable manually adding code, consider encapsulating this logic in a simple plugin or using one like Code Snippets.

Thanks for reading 🙏, I hope you will get this code snippet helpful for your project.

This method helps you Enable or Disable Payment Methods Based on Product Categories while keeping customers happy! 🎉

Would you like help customizing it further? Contact Us

Leave a Reply

Your email address will not be published. Required fields are marked *

Select your currency