在创建会话之前,使用Url变量预填充Woocommerce结帐字段 [英] Pre-fill Woocommerce checkout fields with Url variable before session created
问题描述
我有一个运行woocommerce并带有一些产品的wordpress网站。我想向客户发送其产品的网址,其中包括他们的姓名和电子邮件,以便我可以在woocommerce结帐页面上预先填写姓名和电子邮件。该链接会将客户带到产品页面,他们可以在其中查看产品详细信息并单击添加到购物车。
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".
链接示例: http ://example.com/product/myitem/tu_em=name@example.com& tu_name = theFirstName
我尝试使用用网址预填充Woocommerce结帐字段变量保存在会话中答案代码,但是您已经需要创建woocommerce会话,直到您单击添加到购物车,该会话才会发生
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"
我如何
推荐答案
您可以使用使用会话中保存的Url变量预填充Woocommerce结帐字段
an在添加到购物车操作之前,使用以下代码先修改代码并初始化会话cookie:
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 );
}
代码进入活动子主题(或活动主题)的function.php文件。经过测试,可以正常工作。
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于在创建会话之前,使用Url变量预填充Woocommerce结帐字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!