'Make checkout and my account company name filed read only. Make email address for my account read only in Woocommerce

Need to make an company name input read only in the checkout of WooCommerce and also in the My Account addresses (/my-account/edit-address/billing/) read only. This code below is working to make the email address read only and the company name on checkout but is not creating a read only field in Company Name in (/my-account/edit-address/billing/). I have this code in funtions.php. I am not sure if there is simpler way of doing these things with the code provided.

function wc_remove_checkout_fields( $fields ) {

// Billing fields
// unset( $fields['billing']['billing_company'] );
unset( $fields['billing']['billing_email'] );

// Shipping fields
unset( $fields['shipping']['shipping_company'] );

// Order fields

return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'wc_remove_checkout_fields' );


add_filter( 'woocommerce_checkout_fields' , 'custom_checkout_fields' );
function custom_checkout_fields( $fields ) {
$fields['billing']['billing_company']['custom_attributes']['readonly'] = 'readonly';

return $fields;
}

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;
}

add_action( 'woocommerce_save_account_details_errors', 'prevent_user_update_email', 10, 2 );

function prevent_user_update_email( &$error, &$user ){
$current_user = get_user_by( 'id', $user->ID );
$current_email = $current_user->user_email;
if( $current_email !== $user->user_email){
    $error->add( 'error', 'E-mail cannot be updated.');
}

add_action( 'wp_footer' , 'make_billing_company_field_readonly' );
function make_billing_company_field_readonly(){
// Only for account fields
if( is_account_page() ): ?>
<script type='text/javascript'>
    jQuery(function($){
        $('form.edit-account input#billing_company').prop('readonly', true );
    });
</script>
<?php endif;
}

}


Solution 1:[1]

wc-address-i18n-js will be enqueued in the my-account edit address page. We can add inline JS using the JS id as shown below. The WP function wp_add_inline_script() can be used in this context.

function wc_myaccount_edit_address() {
    if (is_account_page()):
        if (!wp_script_is('jquery', 'done')) {
            wp_enqueue_script('jquery');
        }
        wp_add_inline_script('wc-address-i18n', 'jQuery(document).ready(function($){$("#billing_company").prop("readonly", true );});');
    endif;
}

add_action('wp_enqueue_scripts', 'wc_myaccount_edit_address');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1