E-commerce stores handling regulated or high-risk products may need to restrict certain payment methods at checkout. This guide explains how to automatically disable credit card payments in WooCommerce when specific products are in the cart, helping support compliance workflows for restricted items.
Issue Background
A WooCommerce store needed a way to prevent credit card payments for certain flagged products. These products required different checkout handling to align with compliance requirements and reduce payment processing risk.
The challenge was that WooCommerce does not include a built-in option to conditionally disable payment gateways based on product-level rules. Managing this manually would not scale well and could easily introduce errors.
Diagnosis
After reviewing the setup, it was clear that there was no dynamic method to flag restricted products and no logic in place to hide credit card gateways when those products were added to the cart.
To solve this cleanly, the implementation needed three parts:
- A way to flag specific products as restricted
- Logic to detect those products in the cart
- A method to disable selected payment gateways dynamically during checkout
Resolution Steps
Step 1: Install and use the Code Snippets plugin (recommended)
Before adding any custom PHP code, it’s best practice to use the Code Snippets plugin. This provides a safe, organized, and easily reversible way to add custom functionality without modifying your theme’s functions.php file.
- Install and activate the Code Snippets plugin
- Go to Snippets → Add New
- Create a new snippet for each section of code below, or combine them into one
- Set the snippet to run everywhere
This approach makes troubleshooting easier and prevents your custom code from being lost during theme updates.
Step 2: Add a custom product flag
Create a way to mark products that should trigger payment restrictions. This can be done with Advanced Custom Fields or a custom WooCommerce field.
Example using native WooCommerce hooks:
add_action('woocommerce_product_options_general_product_data', 'add_bram_checkbox');
function add_bram_checkbox() {
woocommerce_wp_checkbox(array(
'id' => 'bram_product',
'label' => __('Is this product restricted?', 'woocommerce'),
'description' => __('Check this box to disable credit card payments for this product.', 'woocommerce')
));
}
Save the value when the product is updated:
add_action('woocommerce_process_product_meta', 'save_bram_checkbox');
function save_bram_checkbox($post_id) {
$value = isset($_POST['bram_product']) ? 'yes' : 'no';
update_post_meta($post_id, 'bram_product', $value);
}
Step 3: Detect restricted products and disable payment gateways
Use the woocommerce_available_payment_gateways filter to conditionally remove payment methods such as credit cards when restricted products are in the cart.
add_filter('woocommerce_available_payment_gateways', 'restrict_payment_gateways_for_flagged_products');
function restrict_payment_gateways_for_flagged_products($available_gateways) {
if (is_admin() || !is_checkout()) {
return $available_gateways;
}
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
$is_restricted = get_post_meta($product_id, 'bram_product', true);
if ($is_restricted === 'yes') {
// Remove specific gateways (replace with your gateway IDs)
unset($available_gateways['nmi']);
unset($available_gateways['truvo']);
wc_add_notice(
__('Certain items in your cart cannot be purchased using credit card. Please choose an alternative payment method.', 'woocommerce'),
'notice'
);
break;
}
}
return $available_gateways;
}
Step 4: Test the functionality
Before deploying to production, verify that products marked as restricted trigger the payment restriction, that credit card gateways are removed only when needed, that alternative payment methods remain available, and that checkout messaging is clear to customers.
Step 5: Deploy and monitor
Once testing is complete, deploy the changes to the live environment and monitor checkout behavior to confirm the logic works as expected.
Final Outcome
By implementing a product-level restriction flag and dynamically filtering payment gateways, the store can automatically enforce payment restrictions for regulated products, reduce compliance risk, improve checkout clarity, and eliminate the need for manual oversight.
This pattern is also adaptable for other WooCommerce use cases, including geographic restrictions, subscription rules, and product-specific checkout conditions.
If you need help implementing advanced WooCommerce logic, payment gateway restrictions, or custom checkout workflows, contact Freshy.