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”?
chrishajer
(Chris (Gravity Forms))
November 10, 2021, 5:33pm
2
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?
chrishajer
(Chris (Gravity Forms))
November 10, 2021, 6:02pm
4
Hi David. First, be sure that Gravity Forms logging is enabled.
For quite a while, enabling logging within Gravity Forms has been a go-to method for debugging any issues within Gravity Forms. As of Gravity Forms version 2.2, logging functionality has been moved from a separate add-on to becoming a core feature of...
Est. reading time: 4 minutes
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.
system
(system)
Closed
December 11, 2021, 10:13am
6
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.