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:
$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 );
Firstly, go to /plugins/woocommerce/templates/checkout
, copy the form-billing.php
template file found in that folder and paste it in yourTheme/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:
<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>
To this:
<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>
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:
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');