Using Gravity Forms ‘gform_coupons_can_apply_coupon’ PHP filter

The gform_coupons_can_apply_coupon Gravity Forms PHP filter allows custom logic to be used to determine if a 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.

More information

See Gravity Forms Docs: gform_coupons_can_apply_coupon

Examples

Restrict a coupon to a single form

This 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

This 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 );

Place this code in the functions.php file of your active theme.

This filter was added in Coupons 2.9.

Source Code: This filter is located in apply_coupon_code() in class-gf-coupons.php.