Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When people enter the my woocommerce shop following a link in an sales email with email and name as parameters I would like to prefill the name and email in the checkout page.

Therefore I created an action and filter. This works as expected but only if I do a hard refresh on the sales page (ctrl + f5)

I've excluded the sales page and the checkout page from the cache and varnish cache but this didn't fix the issue.

Am I missing something here? Do you have any idea why this only works with a hard refresh?

Any help is greatly appreciated.

Code:

    function save()
    {
    if ( is_page( 'sales-page' ) )
    {
        if ( isset( $_GET['tu_em'] ) ) {
            global $woocommerce;
            $woocommerce->session->set( 'tu_em', $_GET['tu_em'] );
        }
        if ( isset( $_GET['tu_name'] ) ) {
            global $woocommerce;
            $woocommerce->session->set( 'tu_name', $_GET['tu_name'] );
        }
    }
}
add_action( 'wp_enqueue_scripts', 'save_email' , 1100);

function override_checkout_email_field( $fields ) {
    global $woocommerce;
    $email = $woocommerce->session->get('tu_em');
    if(!is_null($email)) {
      $fields['billing']['billing_email']['default'] = $email;
    }
    $name = $woocommerce->session->get('tu_name');
    if(!is_null($name)) {
      $fields['billing']['billing_first_name']['default'] = $name;
    }
    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'override_checkout_email_field' );
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
497 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here below, you will find the correct working code, to save user data from an URL (GET) into WooCommerce sessions and to autofill with that data the related checkout fields.

URL will be like: http://example.com/sales-page/?tu_em=name@example.com&tu_name=theFirstName

This can be done from any URL as the code will detect the URL variable, when they are set.

The code;

// Save user data from URL to Woocommerce session
add_action( 'template_redirect', 'set_custom_data_wc_session' );
function set_custom_data_wc_session () {
    if ( isset( $_GET['tu_em'] ) || isset( $_GET['tu_name'] ) ) {
        $em   = isset( $_GET['tu_em'] )   ? esc_attr( $_GET['tu_em'] )   : '';
        $name = isset( $_GET['tu_name'] ) ? esc_attr( $_GET['tu_name'] ) : '';

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

// Autofill checkout fields 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'];

    // Name
    if( isset($data['name']) && ! empty($data['name']) )
        $address_fields['billing_first_name']['default'] = $data['name'];

    return $address_fields;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...