当人们按照销售电子邮件中的链接进入我的woocommerce商店时,我会在结帐页面预先填写姓名和电子邮件。
因此我创建了一个动作和过滤器。这可以按预期工作,但前提是我在销售页面上进行了硬刷新(ctrl + f5)
我已从缓存和清漆缓存中排除了销售页面和结帐页面,但这并没有解决问题。
我在这里遗漏了什么吗?你知道为什么这只能用于刷新硬盘吗?
非常感谢任何帮助。
代码:
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' );
答案 0 :(得分:1)
下面,您将找到正确的工作代码,将用户数据从URL(GET)保存到woocommerce会话中,并使用该数据自动填充相关的结帐字段。
网址如下:http://example.com/sales-page/?tu_em=name@example.com&tu_name=theFirstName
这可以从任何URL完成,因为代码将在设置它们时检测URL变量。
代码;
// 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文件。经过测试并正常工作。
答案 1 :(得分:0)
发货信息(发送到另一个地址)默认是自动的,我们将其用于自动填充账单字段
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;
}