Add billing fields to user registration

Adds billing fields to the My Account user registration section. Limited to shop base country due to that feature requiring asynchronous state/province field updating (wp_ajax_nopriv updating select field from $countries->get_states( $country )).

For more advanced functionality check out the official extension:
Custom User Registration Fields for WooCommerce

// Add Billing Fields To Registration Form
add_action( 'woocommerce_register_form_start', function() {

	$countries = new WC_Countries();
	$billingfields = $countries->get_address_fields();
	unset( $billingfields['billing_email'] );
	$fields = apply_filters( 'woocommerce_billing_fields', $billingfields );

	foreach( $fields as $key => $field_args ) {
		woocommerce_form_field( $key, $field_args );
	}

}, 15 );

// Billing Fields Registration Validation
add_action( 'woocommerce_register_post', function( $username, $email, $validation_errors ) {

	if( empty( $_POST['billing_first_name'] ) ) {
		$validation_errors->add(
			'billing_first_name_error', __( 'Please provide a first name.', 'woocommerce' )
		);
	}

	if( empty( $_POST['billing_last_name'] ) ) {
		$validation_errors->add(
			'billing_last_name_error', __( 'Please provide a last name.', 'woocommerce' )
		);
	}

	if( empty( $_POST['billing_country'] ) ) {
		$validation_errors->add(
			'billing_country_error', __( 'Please select a country.', 'woocommerce' )
		);
	}

	if( empty( $_POST['billing_address_1'] ) ) {
		$validation_errors->add(
			'billing_address_1_error', __( 'Please provide a street address.', 'woocommerce' )
		);
	}

	if( empty( $_POST['billing_city'] ) ) {
		$validation_errors->add(
			'billing_city_error', __( 'Please provide a city or region.', 'woocommerce' )
		);
	}

	if( empty( $_POST['billing_state'] ) ) {
		$validation_errors->add(
			'billing_state_error', __( 'Please select a state or province.', 'woocommerce' )
		);
	}

	if( empty( $_POST['billing_postcode'] ) ) {
		$validation_errors->add(
			'billing_postcode_error', __( 'Please provide a postal code.', 'woocommerce' )
		);
	}

	if( empty( $_POST['billing_phone'] ) ) {
		$validation_errors->add(
			'billing_phone_error', __( 'Please provide a phone number.', 'woocommerce' )
		);
	}

	return $validation_errors;

}, 10, 3 );

// Save Billing Fields At Registration
add_action( 'woocommerce_created_customer', function( $customer_id ) {

	if( isset( $_POST['billing_first_name'] ) ) {
		update_user_meta(
			$customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] )
		);
		update_user_meta(
			$customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] )
		);
	}

	if( isset( $_POST['billing_last_name'] ) ) {
		update_user_meta(
			$customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] )
		);
		update_user_meta(
			$customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] )
		);
	}

	if( isset( $_POST['billing_company'] ) ) {
		update_user_meta(
			$customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] )
		);
	}

	if( isset( $_POST['billing_country'] ) ) {
		update_user_meta(
			$customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] )
		);
	}

	if( isset( $_POST['billing_address_1'] ) ) {
		update_user_meta(
			$customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] )
		);
	}

	if( isset( $_POST['billing_address_2'] ) ) {
		update_user_meta(
			$customer_id, 'billing_address_2', sanitize_text_field( $_POST['billing_address_2'] )
		);
	}

	if( isset( $_POST['billing_city'] ) ) {
		update_user_meta(
			$customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] )
		);
	}

	if( isset( $_POST['billing_state'] ) ) {
		update_user_meta(
			$customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] )
		);
	}

	if( isset( $_POST['billing_postcode'] ) ) {
		update_user_meta(
			$customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] )
		);
	}

	if( isset( $_POST['billing_phone'] ) ) {
		update_user_meta(
			$customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] )
		);
	}

}, 10, 1 );

Instructions for Add billing fields to user registration

  1. Log into a staging or locally hosted clone of your site.
  2. Install and activate Code Snippets plugin.
  3. WP Admin > Snippets > Add New
  4. Copy and paste the code from the section above.
  5. Check to ensure formatting came over properly.
  6. Customize the code as desired.
  7. Add a meaningful title.
  8. Select whether to run on front-end or back-end (or both).
  9. Click “Save and Activate”.
  10. Test your site to ensure it works.
  11. Disable if any problems, or recover.
  12. Repeat for live environment.

Need help modifying Add billing fields to user registration?

Contact me. I can help with fitting projects or refer to my partner.

License

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.

Disclaimer of warranty
WP Engine - A smarter way to WordPress
The best email marketing tool, responsive templates, automations, Worldwide support, tracking and reports, Benchmark Email, free plan available
Sell everywhere. Use Shopify to sell in-store and online.
Codeable - Hire the best WordPress freelancers - starting at $80 USD/hr - Money-back guarantee
Klaviyo partner badge
Okendo Partner, certified
WooCommerce, the most customizable eCommerce platform for building your online business. Click to get started.
Jetpack, a stronger, customizable site without sacrificing safety. Click to get started.