Only Allow Coupon Code IF Radio Button in Field is selected

I have Form 9 with a radio field (field 8) containing three options. Regardless of which of the three options are selected, I want to display the Coupon Code Field. However, I have one coupon code in particular (MYHOME150) that should only be available if the second option in field 8 (8_1) is selected. I can’t figure out how to make it work.

I found this on GF website as a starting point:

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

However, the above code is checking if the form being accessed is 1. In my case, I need to check if a radio button is selected. I don’t know how to update the code to check if 8_1 is selected on form 9. Any help would be appreciated. If it matters, the Label of the 8_1 option is VIP Package.

You need to obtain the value of the radio button and assign it to a variable. Then include that as one of the conditions. Please notice the update to form 9 and coupon code IS EQUAL to ‘MYHOME150’ (rather than not equal to). Keep in mind, this code rejects the coupon code if these conditions aren’t met, so the final condition is if the radio button IS NOT EQUAL to the value of radio button option 2.

add_filter( 'gform_coupons_can_apply_coupon', function ( $can_apply, $coupon_code, $existing_coupon_codes, $feed, $form ) {

	$vip_package = rgar( $entry, '8');
	
    if ( $form['id'] == 9 && $coupon_code == 'MYHOME150' && $vip_package !== 'value for 2nd selection' ) {
        $can_apply['is_valid']       = false;
        $can_apply['invalid_reason'] = 'the error message';
    }
         
    return $can_apply;
}, 10, 5 );
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.