What I am trying to do is change the checkout fields so that if a user is logged in they can’t edit their billing email field.
This code below works great however ideally I want to remove the situation where users are able to have 2 email address in the first place and set the value of $address_fields['billing']['billing_email']
to be the users email address from their account.
I’ve tried adding
'value' => $current_user->user_email
and
'default' => $current_user->user_email
to the array however this appears to do nothing.
So:
- Firstly, how can I set a value for this form and prevent it from being changed.
- Secondly, is
$current_user->user_email
the correct way to get the users account (contact) email and not the billing email from their account.
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 );
2
Answers
Firstly, go to
/plugins/woocommerce/templates/checkout
, copy theform-billing.php
template file found in that folder and paste it inyourTheme/woocommerce/checkout/
.Secondly, prevent WooCommerce from auto-filling the email address field by editing the copied
form-billing.php
template file and changing the following snippet:To this:
Thirdly, inject the WordPress email to the field, make it immutable and add your custom description by adding the following to your theme’s
functions.php
file:Use if you need changing user details while submitting new woo order
This function will compare checkout fields:
with personal account fields:
and will update in case of discrepancy while submitting new order.