問題描述
當人們通過銷售電子郵件中的鏈接以電子郵件和姓名作為參數進入我的 woocommerce 商店時,我想在結帳頁面中預先填寫姓名和電子郵件.
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.
因此我創(chuàng)建了一個動作和過濾器.這按預期工作,但前提是我在銷售頁面上進行了硬刷新 (ctrl + f5)
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?
非常感謝任何幫助.
代碼:
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' );
推薦答案
在下面,您將找到正確的工作代碼,將來自 URL (GET) 的用戶數據保存到 WooCommerce 會話中,并使用該數據自動填充相關結帳字段.
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 將類似于:http://example.com/sales-page/?tu_em=name@example.com&tu_name=theFirstName
這可以從任何 URL 完成,因為代碼會在設置 URL 變量時檢測它們.
This can be done from any URL as the code will detect the URL variable, when they are set.
代碼;
// 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;
}
代碼位于您的活動子主題(或活動主題)的 function.php 文件中.經過測試并有效.
這篇關于使用會話中保存的 Url 變量預填充 Woocommerce 結賬字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!