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

蓝咒 提交于 2019-11-28 14:27:49

Here bellow, 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.

Shipping information(send to another address) by default is auto we use that for autofill billing fildes

add_filter( 'woocommerce_checkout_fields' , 'ahmadyani_checkout_field_defaults', 20 );
function ahmadyani_checkout_field_defaults( $fields ) {
    $user = get_user_meta(get_current_user_id());
    $first_name = $user ? $user['shipping_first_name'][0] : '';
    $last_name = $user ? $user['shipping_last_name'][0] : '';
    $company = $user ? $user['shipping_company'][0] : '';
    $shipping_address_1 = $user ? $user['shipping_address_1'][0] : '';
    $shipping_address_2 = $user ? $user['shipping_address_2'][0] : '';
    $shipping_city = $user ? $user['shipping_city'][0] : '';
    $shipping_state = $user ? $user['shipping_state'][0] : '';
    $shipping_postcode = $user ? $user['shipping_postcode'][0] : '';
    $fields['billing']['billing_first_name']['default'] = $first_name;
    $fields['billing']['billing_last_name']['default'] = $last_name;
    $fields['billing']['billing_company']['default'] = $company;
    $fields['billing']['billing_address_1']['default'] = $shipping_address_1;
    $fields['billing']['billing_address_2']['default'] = $shipping_address_2;
    $fields['billing']['billing_city']['default'] = $shipping_city;
    $fields['billing']['billing_state']['default'] = $shipping_state;
    $fields['billing']['billing_postcode']['default'] = $shipping_postcode;
    return $fields;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!