Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

php - Pre-fill Woocommerce checkout fields with Url variables saved in session

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...