Disable Payment Methods for Specific Products

Last update:

If you’re running an online store with WooCommerce, you might encounter scenarios where you want to restrict certain payment methods for specific products or shipping classes. For instance, you may not want to offer card payment or bank transfer as an option for products that are available for cash on delivery.

By default, WooCommerce offers a range of payment gateway options that you can enable or disable for your store. However, disabling a payment gateway will remove it for all products and shipping classes, which may not always be desirable.

Fortunately, you can use some custom code to disable specific payment gateways for specific products or shipping classes. This can help you better control the payment methods available to your customers and ensure that they have a seamless checkout experience.

The code snippet to disable payment gateways is relatively simple and involves using the woocommerce_available_payment_gateways filter hook to modify the list of available payment gateways.

Here’s an example code snippet that disables card payment for products that have a specific shipping class (e.g., ‘large’):

add_filter( 'woocommerce_available_payment_gateways', 'disable_payment_gateways_for_specific_shipping_classes', 99, 1 );

function disable_payment_gateways_for_specific_shipping_classes( $available_gateways ) {
    global $woocommerce;

    // set the shipping classes for which you want to disable payment gateways
    $disabled_shipping_classes = array( 'large' );

    // check if any of the products in the cart have a disabled shipping class
    $disable_gateways = false;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        if ( has_term( $disabled_shipping_classes, 'product_shipping_class', $product->get_id() ) ) {
            $disable_gateways = true;
            break;
        }
    }

    // if any of the products in the cart have a disabled shipping class, disable the payment gateways
    if ( $disable_gateways ) {
        unset( $available_gateways['stripe'] ); // replace 'stripe' with the ID of your card payment gateway
        unset( $available_gateways['bacs'] ); // replace 'bacs' with the ID of your bank transfer payment gateway
    }

    return $available_gateways;
}

This code snippet works by checking if any of the products in the cart have the ‘large’ shipping class assigned to them. If they do, it disables (i.e., unsets) the Stripe payment gateway, which is identified by the ID ‘stripe’. You can replace ‘stripe’ with the ID of your card payment gateway.

You can use a similar code snippet to disable other payment gateways, such as bank transfer. The process involves identifying the ID of the payment gateway (which can be found in the WooCommerce settings) and replacing ‘stripe’ in the code snippet with the appropriate ID.

One of the key benefits of using this code snippet is that it enables you to provide a more streamlined and personalized checkout experience for your customers. By disabling payment gateways that are not relevant for specific products or shipping classes, you can reduce the clutter and complexity of the checkout page, making it easier for customers to complete their purchase.