[英]Check for existing email in WooCommerce checkout using Ajax in the default “billing_email” field
[英]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 订单时需要更改用户详细信息,请使用<\/strong>
此函数将比较结帐字段:
billing_first_name - billing_last_name - billing_email
带有个人帐户字段:
名字 - 姓氏 - 显示名
并会在提交新订单时出现差异进行更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.