Disable email field on WooCommerce customer accoun

2020-08-09 05:50发布

问题:

I am making a wordpress website i have one problem if someone know about this. what I wanted is that they can register their email upon checkout and then once their account has been completely created, they cannot change their email anymore on the Account Details page. Would that be possible?

Thanks in advance

回答1:

The only way to prevent modifying the email address would be to copy the form-edit-account into the WooCommerce subdirectory of your theme directory and delete the email field from the WooCommerce template \myaccount\form-edit-account.php You'd would then need to remove the email as a required field using this filter. You can't really show the field and prevent users from updating it that is the only issue with this solution. You could add in function that displayed the user's email address somewhere else in the edit account form or on the my account page to get around this.

add_filter('woocommerce_save_account_details_required_fields', 'remove_required_email_address');

function remove_required_email_address( $required_fields ) {
    unset($required_fields['account_email']);

    return $required_fields;
}


回答2:

You can search in google and find too many answers for fast answer you can below code in your function.php

function custom_override_checkout_fields( $fields ) {
   unset($fields['billing']['billing_email']);    
   return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields', 1000, 1 );

see this question https://wordpress.stackexchange.com/questions/261968/disable-email-field-on-woocommerce-customer-account-details



回答3:

I found a way that works for me.

I use a hook in 'woocommerce_after_edit_account_form' to add a bit of javascript at the end of the page, this javascript makes the field "account_email" read only, so the user can't update it.

add_action( 'woocommerce_after_edit_account_form', 'disable_edit_email_address' );

function disable_edit_email_address( ) {
    $script = '<script type="text/javascript">'.
              'var account_email = document.getElementById("account_email");'.
              'if(account_email) { '.
              '     account_email.readOnly = true; '.
              '     account_email.className += " disable-input";'.
              '}'.
              '</script>';
    echo $script;
}

Additionally, you can see, i use the style "disable-input" for opacity the input:

.disable-input {
  opacity: .6;
}

Regards



标签: wordpress