How to Restrict Coupons by Amount

Introduction

Are you interested in how to restrict coupons based on a specified amount? This tutorial will guide you through the process of utilizing a PHP action to prevent coupons from being processed when their value is below a set threshold.

By default, you have the option to create coupons in either a percentage or flat rate format for any amount. However, what if you mistakenly input .75 instead of 75%? This small code snippet can help you avoid such errors in form submissions, and we’ll walk you through the process in this tutorial.

To learn more about using coupons, please review this helpful guide.

Creating the coupon

To start, let’s create a coupon. In this tutorial, we’ll create a coupon with a flat rate fee of .75, even though our intention is to set it at 75%. This is just for demonstration purposes and to highlight how the snippet we’ll be adding can help with validation.

To create a new coupon, navigate to WPForms » Payments » Coupons and click Add New Coupon. Inside the Name field, give your coupon a name that makes sense to you for organizational purposes.

Next, in the Code field you can either manually enter a coupon code or click the Generate Code button to allow WPForms to create this code for you. Inside the Amount field, enter .75 and from the dropdown, be sure to select the currency symbol.

By selecting the currency symbol, you’re utilizing the flat rate discount rather than a percentage.

For this tutorial, we’re not going to set a date or time so we’ll scroll down and select the forms we want to use this coupon for and then click Save Coupon to save your changes.

in order to restrict coupons, we must begin by creating a new coupon

Creating the form

Now that the coupon is set up, it’s time to create the form. For any assistance in how to create forms, please take a look at this step-by-step guide.

create your form and add your fields

Once we’ve added the Coupon field, you’ll notice that the coupon is already assigned.

notice that when you add the coupon field, the allowed coupons are already assigned

Adding the snippet

Now it’s time to add the snippet. If you need any help in how and where to add snippets, please review this tutorial.

/*
 * Do not allow to submit coupons lower than 1.00.
 *
 * @link https://wpforms.com/developers/how-to-restrict-coupons-by-amount/
*/
 
function wpf_discard_low_coupons( $field_id, $field_submit, $form_data ) {
 
    $coupon = wpforms_coupons()->get( 'repository' )->get_coupon_by_code( $field_submit );
 
    if ( $coupon === null ) {
        return;
    }
 
    $currency = wpforms_get_currency();
 
    if ( $coupon->get_discount_type() === 'flat' && (float) $coupon->get_discount_amount() <= 1.00 ) {
        wpforms()->get( 'process' )->errors[ $form_data[ 'id' ] ][ $field_id ] = sprintf( /* translators: %s - Currency code name. */
            esc_html__( 'Coupon amount is less that 1.00 %s. Please, use another one.', 'your-translation-domain' ),
            $currency
        );
    }
}
add_action( "wpforms_process_validate_payment-coupon", 'wpf_discard_low_coupons', 10, 3 );

And that’s all you need, whenever a visitor uses that coupon they will not be able to submit the form.

by using this snippet to restrict coupons, you will see an error on the form when the coupon is used

Action Reference: wpforms_process_validate_payment-coupon