Reputation: 348
I need to make woocommerce my-account/edit-account/ page email, or last name or another field. One field or two fields, ReadOnly.
My solution is edit form-edit-account.php file in plugins/woocommerce/templates/myaccount folder and edit form-edit-account.php file and add readonly to end of tags and work correctly. For example:
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_email"><?php esc_html_e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--email input-text" name="account_email" id="account_email" autocomplete="email" value="<?php echo esc_attr( $user->user_email ); ?>" readonly />
</p>
But I need make this change only from function.php file. Is this possible? I need code for functions.php file.
Upvotes: 2
Views: 1957
Reputation: 253978
You could also use jQuery instead (here for the "billing_mobile_phone" field):
add_action( 'wp_footer' , 'make_phone_field_readonly' );
function make_phone_field_readonly(){
// Only for account fields
if( is_account_page() ): ?>
<script type='text/javascript'>
jQuery(function($){
$('form.edit-account input#billing_mobile_phone').prop('readonly', true );
});
</script>
<?php endif;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Or as in your penultimate question, you will use the hooked function adding readonly
attribute to your additional fields:
// Display the mobile phone field
// add_action( 'woocommerce_edit_account_form_start', 'add_billing_mobile_phone_to_edit_account_form' ); // At start
add_action( 'woocommerce_edit_account_form', 'add_billing_mobile_phone_to_edit_account_form' ); // After existing fields
function add_billing_mobile_phone_to_edit_account_form() {
$user = wp_get_current_user();
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="billing_mobile_phone"><?php _e( 'Mobile phone', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_mobile_phone" id="billing_mobile_phone" value="<?php echo esc_attr( $user->billing_mobile_phone ); ?>" readonly />
</p>
<?php
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
So at all you have 3 alternatives:
Upvotes: 3