Solution:
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');