How to Restrict Coupon Usage Based on Payment Gateway in WooCommerce – 2026

Last updated on December 15th, 2024 at 07:59 am

Restricting coupon usage based on the payment gateway in WooCommerce can help you control promotions and prevent abuse of certain payment methods. For example, you might want to allow coupons for credit card payments but restrict them for cash on delivery (COD).

In this guide, we’ll show you how to restrict coupon usage based on the payment gateway, ensuring that only specific gateways can process certain coupons. We’ll achieve this without using a plugin, and you’ll have full control over which payment methods apply to each coupon.


Step 1: Add a Custom Field for Payment Restrictions

To restrict coupon usage, you’ll first need to add a custom field on the WooCommerce coupon edit page where you can select which payment gateways are allowed for that coupon.

Code to Add a Custom Field on the Coupon Edit Page

Add this code to your theme’s functions.php file or your child theme:

/*
 * Snippet: How to Restrict Coupon Usage Based on Payment Gateway in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1771
* Tested with WooCommerce 10.7.0
* "This function adds a custom field on the coupon edit page for restricted payment gateways"
*/ function wcsuccess_add_payment_gateway_restriction_field( $coupon_id, $coupon ) { woocommerce_wp_multiselect( array( 'id' => 'wcsuccess_restricted_payment_gateways', 'label' => __( 'Allowed Payment Gateways', 'woocommerce' ), 'description' => __( 'Select which payment gateways are allowed to be used with this coupon.', 'woocommerce' ), 'options' => wc_get_payment_gateway_titles(), 'desc_tip' => true, 'value' => get_post_meta( $coupon_id, 'wcsuccess_restricted_payment_gateways', true ) )); } add_action( 'woocommerce_coupon_options', 'wcsuccess_add_payment_gateway_restriction_field', 10, 2 ); /* * Snippet: How to Restrict Coupon Usage Based on Payment Gateway in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1771
* Tested with WooCommerce 10.7.0
* "This function saves the custom payment gateway field for coupons"
*/ function wcsuccess_save_payment_gateway_restriction_field( $post_id ) { $allowed_gateways = isset( $_POST['wcsuccess_restricted_payment_gateways'] ) ? array_map( 'sanitize_text_field', $_POST['wcsuccess_restricted_payment_gateways'] ) : array(); update_post_meta( $post_id, 'wcsuccess_restricted_payment_gateways', $allowed_gateways ); } add_action( 'woocommerce_coupon_options_save', 'wcsuccess_save_payment_gateway_restriction_field' );

Explanation:

  • Custom Field: Adds a multi-select field in the WooCommerce coupon editor.
  • Payment Gateway List: Populates the field with available payment gateways.
  • Data Saving: Saves the selected gateways to the coupon metadata.

Step 2: Restrict Coupons Based on the Payment Gateway

Once you’ve added the custom field, you can now restrict coupon usage based on the payment gateway selected at checkout.

Code to Restrict Coupon Usage

Add this to your functions.php file:

/*
 * Snippet: How to Restrict Coupon Usage Based on Payment Gateway in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1771
* Tested with WooCommerce 10.7.0
* "This function restricts coupon usage based on the selected payment gateway"
*/ function wcsuccess_restrict_coupon_based_on_gateway( $valid, $coupon, $discount ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { return $valid; } $allowed_gateways = get_post_meta( $coupon->get_id(), 'wcsuccess_restricted_payment_gateways', true ); $chosen_payment_method = WC()->session->get( 'chosen_payment_method' ); if ( ! empty( $allowed_gateways ) && ! in_array( $chosen_payment_method, $allowed_gateways ) ) { wc_add_notice( __( 'This coupon is not valid with the selected payment method.', 'woocommerce' ), 'error' ); return false; } return $valid; } add_filter( 'woocommerce_coupon_is_valid', 'wcsuccess_restrict_coupon_based_on_gateway', 10, 3 );

Explanation:

  • Validation Check: Checks the payment gateway against the list of allowed gateways.
  • Dynamic Restriction: Prevents the coupon from being applied if the payment gateway does not match.
  • Error Message: Displays an error message to the customer if the coupon is restricted.
See also  Reordering WooCommerce checkout fields 2026

Step 3: Save the Chosen Payment Method

To ensure the payment gateway is correctly captured during checkout, you need to save it in WooCommerce’s session. This guarantees that the payment method can be accessed for coupon validation.

Code to Store Chosen Payment Method in WooCommerce Session

Add this to your functions.php file:

/*
 * Snippet: How to Restrict Coupon Usage Based on Payment Gateway in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1771
* Tested with WooCommerce 10.7.0
* "This function stores the chosen payment method in the WooCommerce session"
*/ function wcsuccess_store_chosen_payment_gateway() { if ( isset( $_POST['payment_method'] ) ) { WC()->session->set( 'chosen_payment_method', sanitize_text_field( $_POST['payment_method'] ) ); } } add_action( 'woocommerce_checkout_update_order_review', 'wcsuccess_store_chosen_payment_gateway' );

Explanation:

  • Payment Method Tracking: Stores the selected payment method in the WooCommerce session.
  • Data Sanitisation: Ensures the payment method is secure and clean before storage.

Step 4: Display the Restricted Payment Gateways in the Coupon List

For better transparency, you can display the restricted payment gateways in the WooCommerce coupon list.

Code to Display Payment Restrictions on the Coupons List

Add this to your functions.php file:

/*
 * Snippet: How to Restrict Coupon Usage Based on Payment Gateway in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1771
* Tested with WooCommerce 10.7.0
* "This function displays the restricted payment gateways in the coupons list"
*/ function wcsuccess_add_coupon_column( $columns ) { $columns['allowed_payment_gateways'] = __( 'Allowed Payment Gateways', 'woocommerce' ); return $columns; } add_filter( 'manage_edit-shop_coupon_columns', 'wcsuccess_add_coupon_column' ); /* * Snippet: How to Restrict Coupon Usage Based on Payment Gateway in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1771
* Tested with WooCommerce 10.7.0
* "This function populates the allowed payment gateways column in the coupons list"
*/ function wcsuccess_display_coupon_column( $column, $post_id ) { if ( 'allowed_payment_gateways' === $column ) { $allowed_gateways = get_post_meta( $post_id, 'wcsuccess_restricted_payment_gateways', true ); if ( ! empty( $allowed_gateways ) ) { echo implode( ', ', $allowed_gateways ); } else { echo __( 'No restrictions', 'woocommerce' ); } } } add_action( 'manage_shop_coupon_posts_custom_column', 'wcsuccess_display_coupon_column', 10, 2 );

Explanation:

  • New Column: Adds a column to the WooCommerce coupon list showing allowed payment gateways.
  • Column Data: Populates the new column with the gateways assigned to each coupon.
See also  Move email address field to top of Woocommerce checkout page

Example Workflow

  1. Create a Coupon:
    • Go to WooCommerce > Coupons and create a new coupon.
    • In the Allowed Payment Gateways field, select payment methods like Direct Bank Transfer or PayPal.
  2. Apply Coupon:
    • Customers can apply the coupon at checkout.
  3. Validation:
    • If the payment method is restricted, an error message will display.

Use Case

CouponAllowed Payment GatewayAction
20OFFPayPal onlyOnly works with PayPal
15BANKDEALBank Transfer onlyOnly works with Bank Transfer
FREESHIP50All gateways allowedWorks with all payment methods

Conclusion

By following this guide, you can restrict coupon usage in WooCommerce based on the selected payment gateway. This gives you more control over promotions and ensures that specific payment methods are incentivised.

Test these changes in a staging environment before applying them to your live site. For more WooCommerce customisation tips, check out our WooCommerce Visual Hooks Guide or use our wp-config generator for advanced configurations.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×