Get the label for the selected option in a "Product - Radio Button field"?

I have a Product field set up in my form with two choices (accept | £250, decline | £0)

I created the following function in my functions.php file to change the user’s role based on their selection of product.

function set_place_declined( $entry, $form ) {
	$decision = rgar( $entry, '6.1' );
	if ( $decision == 'Decline' ) {
		if( is_user_logged_in() ) {
			$user = wp_get_current_user();
			   if ( in_array( 'offered_a_place', (array) $user->roles ) ) {
					$user->set_role('declined_a_place');
					}
			}		
	}
}
add_action( 'gform_after_submission_8', 'set_place_declined', 10, 2 );

This works if I use a standard drop-down or radio button field, but I cannot get it to work when using the ‘Product’ field.

I read here: Entry Object - Gravity Forms Documentation
That using “6.1” should “return the field label for a single product that has id 6”
So this is what I am trying to do, but it doesn’t seem to be working.
Does this only work for ‘Single Product’ fields?

Is there any way to get the label for the selected option in a “Product - Radio Button field”?

Hi David. Log the $entry to see what that product field looks like, so that you know how to access it.

function set_place_declined( $entry, $form ) {
	GFCommon::log_debug( __METHOD__ . '(): The Entry => ' . print_r( $entry, true ) );
	$decision = rgar( $entry, '6.1' );
	if ( $decision == 'Decline' ) {
		if( is_user_logged_in() ) {
			$user = wp_get_current_user();
			   if ( in_array( 'offered_a_place', (array) $user->roles ) ) {
					$user->set_role('declined_a_place');
					}
			}		
	}
}
add_action( 'gform_after_submission_8', 'set_place_declined', 10, 2 );

Thanks Chris, I’ve added that line, where would I find the log? I can’t seem to find it in any of the three logs from the settings page?

Hi David. First, be sure that Gravity Forms logging is enabled.

Then, test the form with the updated code. You will find that logged information in the Gravity Forms Core log.

Hi Chris,

Thanks for the pointers on the logging.

My submitted field looks like this in the log;

2021-11-11 10:04:20.026779 - DEBUG → log_save_field_value: Input ID: 6. POST value => Accept|250
2021-11-11 10:04:20.026802 - DEBUG → log_save_field_value: Saved value => Accept|250

And my code looks like this;

function offer_of_place( $entry, $form ) {
	GFCommon::log_debug( __METHOD__ . '(): The Entry => ' . print_r( $entry, true ) );
	$decision = rgar( $entry, '6' );
	
	if ( $decision == 'Decline|0' ) {
		if( is_user_logged_in() ) {
			$user = wp_get_current_user();
			   if ( in_array( 'offered_a_place', (array) $user->roles ) ) {
					$user->set_role('declined_a_place');
					}
			}		
	}
	
	else if ( $decision == 'Accept|250' ) {
		if( is_user_logged_in() ) {
			$user = wp_get_current_user();
			   if ( in_array( 'offered_a_place', (array) $user->roles ) ) {
					$user->set_role('accepted_a_place');
					}
			}		
	}

}

add_action( 'gform_after_submission_8', 'offer_of_place', 10, 2 );

The “Decline” option works correctly.
The “Accept” option still does not work.

If I change it to ‘Accept|0’ (and change my product price to zero in my form as well), then it works. So it looks like the problem is related to matching the value of the submitted field, but I can’t work out what the problem is.

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