将 WordPress 用户电子邮件而不是账单电子邮件设置为 WooCommerce 结帐字段的默认值
Set WordPress user email instead of billing email as default value for WooCommerce checkout field
我想做的是更改结帐字段,这样如果用户已登录,他们就无法编辑他们的账单电子邮件字段。
下面的代码效果很好,但理想情况下,我想消除用户首先可以拥有 2 个电子邮件地址的情况,并将 $address_fields['billing']['billing_email']
的值设置为其帐户中的用户电子邮件地址.
我试过添加
'value' => $current_user->user_email
和
'default' => $current_user->user_email
到数组,但这似乎什么也没做。
所以:
- 首先,我如何为这个表格设置一个值并防止它被更改。
- 其次,
$current_user->user_email
是获取用户帐户(联系人)电子邮件的正确方法,而不是从他们的帐户中获取帐单电子邮件的正确方法。
function custom_checkout_fields( $address_fields ) {
$current_user = wp_get_current_user();
if ( is_user_logged_in() ) {
unset( $address_fields['billing']['billing_email'] );
unset( $address_fields['billing']['billing_em_ver'] );
$address_fields['billing']['billing_email'] = [
'label' => 'Email address',
'required' => false,
'description' => '<i><a href="/shop/my-account/customer-logout/">to change this please click here logout and login/register as another user</a> or <a href="/shop/my-account/edit-account/">click here to modify the address on your account</a></i>',
'custom_attributes' => [
'disabled' => 'disabled',
]
];
return $address_fields;
} else{
return $address_fields;
}
}
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields' ,20, 1 );
首先,转到 /plugins/woocommerce/templates/checkout
,复制在该文件夹中找到的 form-billing.php
模板文件并将其粘贴到 yourTheme/woocommerce/checkout/
。
其次,通过编辑复制的 form-billing.php
模板文件并更改以下代码段来阻止 WooCommerce 自动填写电子邮件地址字段:
<div class="woocommerce-billing-fields__field-wrapper">
<?php
$fields = $checkout->get_checkout_fields( 'billing' );
foreach ( $fields as $key => $field ) {
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
}
?>
</div>
为此:
<div class="woocommerce-billing-fields__field-wrapper">
<?php
$fields = $checkout->get_checkout_fields( 'billing' );
foreach ( $fields as $key => $field ) {
if ( 'billing_email' != $key ) {
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
} else {
woocommerce_form_field( $key, $field );
}
}
?>
</div>
第三,将 WordPress 电子邮件插入该字段,使其不可变,并通过将以下内容添加到主题的 functions.php
文件中来添加自定义描述:
function shillongtitude_billing_email_field($fields) {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
//set the user email as the email field value
$fields['billing']['billing_email']['default'] = $current_user->user_email;
//set the description
$fields['billing']['billing_email']['description'] = '<i><a href="/shop/my-account/customer-logout/">to change this please click here logout and login/register as another user</a> or <a href="/shop/my-account/edit-account/">click here to modify the address on your account</a></i>';
// set attributes
$fields['billing']['billing_email']['custom_attributes'] = array( 'readonly'=>'readonly', 'label' => 'Email address', 'required' => false );
return $fields;
} else {
return $fields;
}
}
add_filter('woocommerce_checkout_fields', 'shillongtitude_billing_email_field');
如果您需要在提交新的 woo 订单时更改用户详细信息,请使用
此函数将比较结帐字段:
billing_first_name - billing_last_name - billing_email
个人帐户字段:
first_name - last_name - display_name
如果在提交新订单时出现差异,将会进行更新。
add_action('woocommerce_new_order', 'sync_woocommerce_email', 10, 2) ;
function sync_woocommerce_email( $order_id ) {
$current_user = wp_get_current_user();
$user_cname = $current_user->display_name;
$user_fname = $current_user->first_name;
$user_lname = $current_user->last_name;
$user_fullname = $user_fname.' '.$user_lname;
$bill_fname = $current_user->billing_first_name;
$bill_lname = $current_user->billing_last_name;
$bill_fullname = $bill_fname.' '.$bill_lname;
if ($current_user->user_email != $current_user->billing_email) {
wp_update_user( array ( 'ID' => $current_user->ID, 'user_email' => $current_user->billing_email ) ) ;
}
if ($current_user->display_name != $bill_fullname) {
wp_update_user( array ( 'ID' => $current_user->ID, 'display_name' => $bill_fullname ) ) ;
}
}
我想做的是更改结帐字段,这样如果用户已登录,他们就无法编辑他们的账单电子邮件字段。
下面的代码效果很好,但理想情况下,我想消除用户首先可以拥有 2 个电子邮件地址的情况,并将 $address_fields['billing']['billing_email']
的值设置为其帐户中的用户电子邮件地址.
我试过添加
'value' => $current_user->user_email
和
'default' => $current_user->user_email
到数组,但这似乎什么也没做。
所以:
- 首先,我如何为这个表格设置一个值并防止它被更改。
- 其次,
$current_user->user_email
是获取用户帐户(联系人)电子邮件的正确方法,而不是从他们的帐户中获取帐单电子邮件的正确方法。
function custom_checkout_fields( $address_fields ) {
$current_user = wp_get_current_user();
if ( is_user_logged_in() ) {
unset( $address_fields['billing']['billing_email'] );
unset( $address_fields['billing']['billing_em_ver'] );
$address_fields['billing']['billing_email'] = [
'label' => 'Email address',
'required' => false,
'description' => '<i><a href="/shop/my-account/customer-logout/">to change this please click here logout and login/register as another user</a> or <a href="/shop/my-account/edit-account/">click here to modify the address on your account</a></i>',
'custom_attributes' => [
'disabled' => 'disabled',
]
];
return $address_fields;
} else{
return $address_fields;
}
}
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields' ,20, 1 );
首先,转到 /plugins/woocommerce/templates/checkout
,复制在该文件夹中找到的 form-billing.php
模板文件并将其粘贴到 yourTheme/woocommerce/checkout/
。
其次,通过编辑复制的 form-billing.php
模板文件并更改以下代码段来阻止 WooCommerce 自动填写电子邮件地址字段:
<div class="woocommerce-billing-fields__field-wrapper">
<?php
$fields = $checkout->get_checkout_fields( 'billing' );
foreach ( $fields as $key => $field ) {
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
}
?>
</div>
为此:
<div class="woocommerce-billing-fields__field-wrapper">
<?php
$fields = $checkout->get_checkout_fields( 'billing' );
foreach ( $fields as $key => $field ) {
if ( 'billing_email' != $key ) {
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
} else {
woocommerce_form_field( $key, $field );
}
}
?>
</div>
第三,将 WordPress 电子邮件插入该字段,使其不可变,并通过将以下内容添加到主题的 functions.php
文件中来添加自定义描述:
function shillongtitude_billing_email_field($fields) {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
//set the user email as the email field value
$fields['billing']['billing_email']['default'] = $current_user->user_email;
//set the description
$fields['billing']['billing_email']['description'] = '<i><a href="/shop/my-account/customer-logout/">to change this please click here logout and login/register as another user</a> or <a href="/shop/my-account/edit-account/">click here to modify the address on your account</a></i>';
// set attributes
$fields['billing']['billing_email']['custom_attributes'] = array( 'readonly'=>'readonly', 'label' => 'Email address', 'required' => false );
return $fields;
} else {
return $fields;
}
}
add_filter('woocommerce_checkout_fields', 'shillongtitude_billing_email_field');
如果您需要在提交新的 woo 订单时更改用户详细信息,请使用
此函数将比较结帐字段:
billing_first_name - billing_last_name - billing_email
个人帐户字段:
first_name - last_name - display_name
如果在提交新订单时出现差异,将会进行更新。
add_action('woocommerce_new_order', 'sync_woocommerce_email', 10, 2) ;
function sync_woocommerce_email( $order_id ) {
$current_user = wp_get_current_user();
$user_cname = $current_user->display_name;
$user_fname = $current_user->first_name;
$user_lname = $current_user->last_name;
$user_fullname = $user_fname.' '.$user_lname;
$bill_fname = $current_user->billing_first_name;
$bill_lname = $current_user->billing_last_name;
$bill_fullname = $bill_fname.' '.$bill_lname;
if ($current_user->user_email != $current_user->billing_email) {
wp_update_user( array ( 'ID' => $current_user->ID, 'user_email' => $current_user->billing_email ) ) ;
}
if ($current_user->display_name != $bill_fullname) {
wp_update_user( array ( 'ID' => $current_user->ID, 'display_name' => $bill_fullname ) ) ;
}
}