Skip to content
Advertisement

Pre-fill Woocommerce checkout fields with Url variable before session created

I have wordpress site running woocommerce with a few products. I want to send my customer a url to their product which includes their name and email so i can pre-fill name and email on woocommerce checkout page. the link will take customer to the product item page, where they can view product details and click “add to cart”.

Link example: http://example.com/product/myitem/tu_em=name@example.com&tu_name=theFirstName

I tried to use Pre-fill Woocommerce checkout fields with Url variables saved in session answer code but you need to already have a woocommerce session created, which doesnt happen until you click “add to cart”

How can i pre-fill my checkout fields page from the url to a product item?

Advertisement

Answer

You can use Pre-fill Woocommerce checkout fields with Url variables saved in session answer code and initialize a session cookie before add to cart action, using the following:

add_action( 'woocommerce_init', 'initiate_customer_session_cookie' )
function initiate_customer_session_cookie(){
    // Ensure that Woocommerce session cookie is initiated
    if ( isset(WC()->session) && ! WC()->session->has_session() ) 
        WC()->session->set_customer_session_cookie( true );
}

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement