skip to Main Content

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:

  1. Firstly, how can I set a value for this form and prevent it from being changed.
  2. 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


  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');
    Login or Signup to reply.
  2. Use if you need changing user details while submitting new woo order


    This function will compare checkout fields:

    billing_first_name – billing_last_name – billing_email

    with personal account fields:

    first_name – last_name – display_name

    and will update in case of discrepancy while submitting new order.

    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 ) ) ;
            }
             
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search