// 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 );
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
How to use
- Log into a staging, development, or locally hosted clone of your site
- Install and activate Code Snippets
- WP Admin > Snippets > Add New
- Copy and paste the code from the Description tab above
- Check to ensure formatting came over properly and no syntax errors show up in the editor
- Customize the code as desired
- Add a meaningful title
- Select whether to run on front-end or back-end (or both)
- Click “Save and Activate”
- Test your site to ensure it works
- Disable if any problems, or recover
- Repeat for live environment
License
All code snippets are licensed GPLv2 (or later) matching WordPress licensing.
Disclaimer of warranty:
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.
Support
- Describe the issue and what you’ve observed.
- Describe your expected outcome(s).
- List steps to reproduce the issue.
- Optionally provide screen-shot or video URLs.
- Email to [email protected]
