skip to Main Content

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

}

2

Answers


  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');
    
    Login or Signup to reply.
  2. If you don’t need a fallback for empty fields you can use this easy code:

    function add_readonly_billing_account_fields ( $fields ) {
        // Only my account billing address for logged in users
        if ( is_user_logged_in() && ( is_account_page() || is_checkout() ) ) {
    
            $readonly = ['readonly' => 'readonly'];
            $fields['billing_email']['custom_attributes'] = $readonly;
            $fields['billing_company']['custom_attributes'] = $readonly;
        }
        return $fields;
    }
    add_filter( 'woocommerce_billing_fields', 'add_readonly_billing_account_fields', 25, 1 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search