WooCommerce: Read-only Checkout Fields

Especially for B2B sites, it’s likely that customers are managed by the store admin and are not allowed to change their billing/shipping address unless they request to update it.

But even if you’re simply curious, there is a way to turn each checkout field into read-only inputs. In this way, the saved billing and shipping address will load, and the logged in customer won’t be able to change any data before checkout.

A little note: country and state are dropdowns, and this means adding the “readonly” attribute won’t stop you from changing the selected value. However, if we turn all fields to input type “text”, this problem will go away. And this is why you find two statements in the snippet below; first we turn the field into a text input, and then we make it read-only.

Enjoy!

All billing and shipping fields are now readonly, including the country dropdown (which was a dropdown originally)!

PHP Snippet: Add “readonly” Attribute To The WooCommerce Checkout Billing & Shipping Fields

/**
 * @snippet       Read-only Billing & Shipping @ Woo Checkout
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_checkout_fields', 'bbloomer_checkout_fields_readonly', 9999 );

function bbloomer_checkout_fields_readonly( $fields ) {
	foreach ( $fields['billing'] as $field_key => $field_atts ) {
		$fields['billing'][$field_key]['type'] = 'text';
		$fields['billing'][$field_key]['custom_attributes'] = array( 'readonly' => 'readonly' );
	}
	foreach ( $fields['shipping'] as $field_key => $field_atts ) {
		$fields['shipping'][$field_key]['type'] = 'text';
		$fields['shipping'][$field_key]['custom_attributes'] = array( 'readonly' => 'readonly' );
	}
	return $fields;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *