DEV Community

Cover image for Hide a WooCommerce payment method for one product: the PHP filter still works on the Checkout block
Ali Nawaz
Ali Nawaz

Posted on Originally published at bealinawaz.com AI-assisted

Hide a WooCommerce payment method for one product: the PHP filter still works on the Checkout block

"Don't offer cash on delivery when this product is in the cart." Most snippets for this were written for the shortcode checkout, and many wrap the logic in if ( is_checkout() ). When people move to the Checkout block and the old snippet stops behaving, the usual guess is that block checkout needs JavaScript now.

It doesn't. The same PHP filter, woocommerce_available_payment_gateways, drives the Checkout block. It also does something a JavaScript filter can't do: it makes the server refuse the order. Here's how the filter gets from PHP to the block, and a snippet that relies on it.

How the Checkout block gets its payment list

Four steps, all in WooCommerce core:

  1. WC_Payment_Gateways::get_available_payment_gateways() keeps the gateways whose is_available() returns true, then passes the list through apply_filters( 'woocommerce_available_payment_gateways', ... ). (class-wc-payment-gateways.php)
  2. The Store API cart response, which the Cart and Checkout blocks read, has a payment_methods field built from that same function. (CartSchema.php)
  3. In the browser, the block only shows a regular payment method if its name is in payment_methods. Only then does it call the method's own canMakePayment(). (check-payment-methods.ts)
  4. When the order is placed, the Store API checkout route calls get_available_payment_gateways() again. If the submitted method isn't in the list, the request fails with woocommerce_rest_checkout_payment_method_disabled. (Checkout.php)

So the method disappears from the screen, and a hand-crafted request that still sends cod gets rejected.

The snippet

This hides cash on delivery and bank transfer when the cart has product 123 or 456. Put it in a small plugin or a child theme's functions.php.

<?php
/**
 * Plugin Name: Hide Payment Methods by Product
 * Description: Hides selected payment gateways when the cart contains certain products.
 */

add_filter( 'woocommerce_available_payment_gateways', 'an_hide_gateways_for_products' );

function an_hide_gateways_for_products( $gateways ) {
    // Leave the admin screens alone, but still run on AJAX requests.
    if ( is_admin() && ! wp_doing_ajax() ) {
        return $gateways;
    }

    // No cart in this request (cron, REST calls outside the Store API, and so on).
    if ( ! function_exists( 'WC' ) || null === WC()->cart ) {
        return $gateways;
    }

    $blocked_ids      = array( 123, 456 );     // Product or variation IDs.
    $gateways_to_hide = array( 'cod', 'bacs' ); // Gateway IDs.

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product_id   = (int) $cart_item['product_id'];
        $variation_id = (int) $cart_item['variation_id'];

        if ( in_array( $product_id, $blocked_ids, true ) || in_array( $variation_id, $blocked_ids, true ) ) {
            foreach ( $gateways_to_hide as $gateway_id ) {
                unset( $gateways[ $gateway_id ] );
            }
            break;
        }
    }

    return $gateways;
}
Enter fullscreen mode Exit fullscreen mode

A few details:

  • There's no is_checkout() guard. On the block checkout the list comes from Store API REST requests, not from a checkout page load, so a page check isn't a reliable test. The cart check is what matters.
  • is_admin() alone would be wrong too, because it's true for admin-ajax.php requests. Hence is_admin() && ! wp_doing_ajax().
  • product_id is always the parent. The chosen variation is in variation_id (0 for simple products), so checking both lets you block a single variation.
  • The built-in gateway IDs are cod, bacs and cheque. For other gateways, find the ID under WooCommerce > Settings > Payments.

For a category rule, swap the ID check for has_term( $blocked_categories, 'product_cat', $cart_item['product_id'] ). It matches only the categories a product is directly assigned to, not parent categories.

Check it without guessing

Open a private window, add a blocked product, and visit /wp-json/wc/store/v1/cart. If payment_methods still lists cod, the filter isn't running: the plugin isn't active, the function name clashes, or a cache plugin is serving the Store API. Remove the product and reload. The method should be back.

What it won't cover

  • Express buttons. In check-payment-methods.ts, express methods (Apple Pay, Google Pay and the like) skip the payment_methods check and decide through their own canMakePayment(). Test them separately.
  • The order-pay page. "Pay for order" links pay for an existing order, not the cart, so a cart-based rule won't see the product there.
  • Hiding everything. If you remove every gateway for a product, nobody can buy it.

Why not a JavaScript filter?

WooCommerce documents registerPaymentMethodExtensionCallbacks from @woocommerce/blocks-registry. It's useful when the rule depends on something that only exists in the browser. But it only changes what the shopper sees. The checkout request still goes to the server, and the server checks the PHP list from step 4. For a product rule, the PHP filter is the right tool.

The full post has the category snippet in full, a five-step test plan for the block checkout, and an FAQ on Stripe, PayPal and variations: WooCommerce: hide a payment method for a specific product.

Top comments (0)