Cash on Delivery (COD) is a convenient payment option for many WooCommerce stores, but it may not be suitable for every product. High-value items, digital goods, made-to-order products, and items with a higher risk of returns may require customers to pay in advance.
WooCommerce does not include a built-in setting to disable Cash on Delivery for individual products. However, you can add this restriction using a small PHP snippet without installing an additional plugin.
In this tutorial, you’ll learn how to automatically hide Cash on Delivery at checkout whenever a restricted product is added to the cart. Other available payment methods will remain available.
What This WooCommerce Snippet Does
- Checks the products in the customer’s cart.
- Compares product IDs against a list of restricted products.
- Hides Cash on Delivery when a restricted product is present.
- Supports simple products and variable products by checking the parent product ID.
- Leaves other available payment gateways unchanged.
- Works without an additional WooCommerce plugin.
WooCommerce PHP Snippet to Disable COD for Specific Products
Add the following PHP snippet to your child theme’s functions.php file or use a PHP code snippets plugin.
/**
* Disable Cash on Delivery for selected WooCommerce products.
*/
add_filter(
'woocommerce_available_payment_gateways',
'gh_disable_cod_for_selected_products'
);
function gh_disable_cod_for_selected_products( $available_gateways ) {
// Skip changes in the regular WordPress admin.
if ( is_admin() && ! wp_doing_ajax() ) {
return $available_gateways;
}
// Make sure the WooCommerce cart is available.
if ( ! WC()->cart ) {
return $available_gateways;
}
// Replace these example IDs with your product IDs.
$restricted_products = array(
101,
205,
330,
);
foreach ( WC()->cart->get_cart() as $cart_item ) {
// This is the parent product ID for a variation.
$product_id = (int) $cart_item['product_id'];
if ( in_array( $product_id, $restricted_products, true ) ) {
// Remove the standard WooCommerce COD gateway.
if ( isset( $available_gateways['cod'] ) ) {
unset( $available_gateways['cod'] );
}
break;
}
}
return $available_gateways;
}
Important: Replace the example IDs 101, 205, and 330 with the actual WooCommerce product IDs you want to restrict. The standard WooCommerce Cash on Delivery gateway uses the ID cod. A third-party payment plugin may use a different gateway ID.
How the WooCommerce COD Restriction Works
The snippet uses the woocommerce_available_payment_gateways filter to modify the payment methods WooCommerce makes available. It checks the products in the cart against the IDs in the $restricted_products array.
If a matching product is found, the snippet removes the standard COD gateway from the available payment methods. Customers can then choose from the remaining payment options enabled in your store.
The restriction applies to the entire order. If the cart contains one restricted product and several unrestricted products, Cash on Delivery will still be hidden for the whole order.
How to Find WooCommerce Product IDs
- Log in to your WordPress dashboard.
- Go to Products > All Products.
- Hover over the product you want to restrict.
- Find the product ID displayed beneath the product name.
- Add that ID to the
$restricted_productsarray in the snippet.
For example, if the product ID is 456, update the array like this:
$restricted_products = array(
456,
205,
330,
);
Where Should You Add This Code?
You can add the snippet using either of these methods:
- Child theme: Add the code to your child theme’s
functions.phpfile. Avoid editing the parent theme directly because theme updates can overwrite your changes. - Code snippets plugin: Create a PHP snippet and configure it to run on the storefront. Follow the plugin’s instructions about whether to include the opening PHP tag.
Back up your website before changing code. If possible, test the snippet on a staging site before adding it to your live store.
How to Test the COD Restriction
- Add an unrestricted product to your cart.
- Proceed to checkout and confirm that Cash on Delivery is available if the gateway is enabled and otherwise eligible.
- Remove the product and add one of the restricted products.
- Allow checkout to refresh its available payment methods.
- Confirm that Cash on Delivery is no longer displayed.
- Add an unrestricted product alongside the restricted product and verify that COD remains hidden for the entire order.
If Cash on Delivery still appears, check the product IDs, confirm that your gateway uses the ID cod, and review whether a payment plugin or custom checkout implementation uses a different gateway or payment-handling process.
Can You Disable COD for Variable Products?
For standard WooCommerce variable products, the cart item’s product_id is the parent product ID. Adding that parent ID to the restricted list will normally disable COD for all its variations.
If you want to restrict only specific variations while allowing COD for other variations of the same product, the code must also check the cart item’s variation_id.
Frequently Asked Questions
Does this snippet disable COD for every product?
No. Cash on Delivery is hidden only when the cart contains a product whose ID appears in the $restricted_products array. Other eligible carts can still use COD.
Can customers still use other payment methods?
Yes. The snippet removes only the standard COD gateway. Other payment gateways remain available if they are enabled and meet their own checkout requirements.
What happens if a cart contains several products?
If at least one product is on the restricted list, Cash on Delivery is hidden for the entire order.
Can I restrict COD without adding code?
Yes. You can use a WooCommerce payment restriction plugin that supports product-based payment rules. Check compatibility with your checkout setup before installing one.
Conclusion
Disabling Cash on Delivery for selected WooCommerce products gives you more control over how customers pay for certain orders. With this PHP snippet, you can maintain a list of restricted products and hide the standard COD option whenever one of those products is in the cart.
Replace the example product IDs, add the snippet safely, and test your checkout before using it on your live store.