Gravity Form Set cookie and submit to checkout field


I’m having difficulty with the following code in that I’m adding it to code snippet plugin but I am unable to see the cookie being set in the developer tools after the form has been submitted. My understanding is that if the cookie is set then it should show the cookie name in the session within Developer Tools.

// Saving the user data from Gravity Form field to Woocommerce session
add_action( 'gform_after_submission', 'store_email_insession', 10, 2 );
function store_email_insession( $entry ) {
    $email = rgar( $entry, '3' ); // Get value of field id 3.

    // Set the session data
    WC()->session->set( 'custom_data', array( 'email' => $email ) );

// Autofill the checkout email field from user data saved in Woocommerce session
add_filter( 'woocommerce_billing_fields' , 'prefill_billing_fields' );
function prefill_billing_fields ( $address_fields ) {
    // Get the session data
    $data = WC()->session->get('custom_data');

    // Email
    if( isset($data['email']) && ! empty($data['email']) )
        $address_fields['billing_email']['default'] = $data['email'];

    return $address_fields;

I’ve also tried to set the specific form from which I’d like the email field to be stored from, by specifying the form:

add_action( 'gform_after_submission_11', 'store_email_insession', 10, 2 );

That’s my attempt and I think the autofill section is correct from what I have gleaned but the setting of the cookie itself so, by my estimation, the storing of the the email address isn’t being set as expected, therefore isn’t populating the billing_email field on checkout

Thanks in advance.

imiweb 4 months 0 Answers 13 views 0

Leave an answer