John Davidson

php - WooCommerce: Make Name and address fields read only

0 comments
Message:


Currently I have billing address set to read only using the following code but I need to add customer first & last name to the read only too. I'm placing this in my theme functions.php file




add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
$current_user = wp_get_current_user();;
$user_id = $current_user->ID;
foreach ( $checkout_fields['billing'] as $key => $field ){
if($key == 'billing_address_1' || $key == 'billing_address_2'){
$key_value = get_user_meta($user_id, $key, true);
if( strlen($key_value)>0){
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}


My Answer:
name field as well:

php
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_first_name']['custom_attributes'] = array( 'readonly' => 'readonly' );
$fields['billing']['billing_last_name']['custom_attributes'] = array( 'readonly' => 'readonly' );
$fields['billing']['billing_address_1']['custom_attributes'] = array( 'readonly' => 'readonly' );
$fields['billing']['billing_address_2']['custom_attributes'] = array( 'readonly' => 'readonly' );
$fields['billing']['billing_city']['custom_attributes'] = array( 'readonly' => 'readonly' );
$fields['billing']['billing_postcode']['custom_attributes'] = array( 'readonly' => 'readonly' );
$fields['billing']['billing_country']['custom_attributes'] = array( 'readonly' => 'readonly' );
$fields['billing']['billing_state']['custom_attributes'] = array( 'readonly' => 'readonly' );

return $fields;
}


This code will make the first name, last name, and address fields in the billing section read-only on the WooCommerce checkout page.

Rate this post

4 of 5 based on 1733 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us