Description
This PHP filter allows custom logic to be used to determine if the coupon code can be applied.
Usage
apply_filters( 'gform_coupons_can_apply_coupon', $can_apply, $coupon_code, $existing_coupon_codes, $feed, $form );
Parameters
- $can_apply array
The coupon validation result.
-
$coupon_code string
The coupon code being validated.
-
$existing_coupon_codes string
The coupon codes which have already been applied.
-
$feed Feed Object
The feed (configuration) for the coupon code being validated.
-
$form Form Object
The current form.
Examples
Restrict a coupon to a single form
The following example restricts the usage of a coupon code to a form with id 1.
add_filter( 'gform_coupons_can_apply_coupon', function ( $can_apply, $coupon_code, $existing_coupon_codes, $feed, $form ) { if ( $form['id'] == 1 && $coupon_code !== 'FREE' ) { $can_apply['is_valid'] = false; $can_apply['invalid_reason'] = 'the error message'; } return $can_apply; }, 10, 5 );
Limit a coupon to logged in users only
The following example restricts the usage of a coupon code only to logged in users.
add_filter( 'gform_coupons_can_apply_coupon', function ( $can_apply, $coupon_code, $existing_coupon_codes, $feed, $form ) { if ( is_user_logged_in() === false && $coupon_code === 'TEST' ) { $can_apply['is_valid'] = false; $can_apply['invalid_reason'] = 'the error message'; } return $can_apply; }, 10, 5 );
Placement
This code should be placed in the functions.php file of your active theme.
Since
This filter was added in Coupons 2.9.
Source Code
This filter is located in apply_coupon_code() in class-gf-coupons.php.