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

本小妞迷上赌 提交于 2019-12-06 14:54:36

问题


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?


回答1:


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 ( ! 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.



来源:https://stackoverflow.com/questions/53975656/pre-fill-woocommerce-checkout-fields-with-url-variable-before-session-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!