How to add new values to the WooCommerce registration form

How to add new value to the WooCommerce registration form

While developing an e-shop for a client, I came across a specific requirement. As this is a B2B shop, the client requested that only logged-in users should be allowed to sell.

However, in order for a user to create an account on his e-shop, he required the Company Name and Company ID as mandatory. The company name is in the Woocommerce e-shop by default, I added the company ID, VAT number and VAT number using the well-known WPify plugin.

So what did I need to achieve in addition:

  1. make Company Name and Company ID a required field
  2. add the following two items to the registration form

 

1. Required company name and registration number.

This went very easily. In 10 seconds I googled the snippet that makes this possible. Here it is:

  1. add_filter( 'woocommerce_billing_fields', 'ts_require_wc_company_field');
  2.    function ts_require_wc_company_field( $fields ) {
  3.       $fields['billing_company']['required'] = true;
  4.       $fields['billing_ic']['required'] = true;
  5.       return $fields;
  6.    }

The filter is very simple. It simply sets the selected $fields field items to TRUE on the flag parameter, making them mandatory. A red asterisk will start to appear instead of (optional). You can, of course, add your own fields, whether billing or shipping, as required and make them mandatory in the order process.
 

2. Adding fields to the registration form

This is a bit worse. However, after much research and unsuccessful attempts, I found a solution. These are three different actions.

  1. add_action( 'woocommerce_register_form', 'misha_add_register_form_field' );
  2. function misha_add_register_form_field(){
  3.     woocommerce_form_field(
  4.         'billing_company',
  5.         array(
  6.             'type'        => 'text',
  7.             'required'    => true, // just adds an "*"
  8.             'label'       => 'Názov firmy'
  9.         ),
  10.         ( isset($_POST['billing_company']) ? $_POST['billing_company'] : '' )
  11.     );
  12.     woocommerce_form_field(
  13.         'billing_ic',
  14.         array(
  15.             'type'        => 'text',
  16.             'required'    => true, // just adds an "*"
  17.             'label'       => 'IČO'
  18.         ),
  19.         ( isset($_POST['billing_ic']) ? $_POST['billing_ic'] : '' )
  20.     );
  21. }
  22.  
  23. add_action( 'woocommerce_register_post', 'misha_validate_fields', 10, 3 );
  24. function misha_validate_fields( $username, $email, $errors ) {
  25.     if ( empty( $_POST['billing_company'] ) ) {
  26.         $errors->add( 'billing_company', 'Toto pole je povinné' );
  27.     }
  28.     if ( empty( $_POST['billing_ic'] ) ) {
  29.         $errors->add( 'billing_ic', 'Toto pole je povinné' );
  30.     }
  31. }
  32.  
  33. add_action( 'woocommerce_created_customer', 'misha_save_register_fields' );
  34. function misha_save_register_fields( $customer_id ){
  35.     if ( isset( $_POST['billing_company'] ) ) {
  36.         update_user_meta( $customer_id, 'billing_company', wc_clean( $_POST['billing_company'] ) );
  37.         update_user_meta( $customer_id, 'billing_ic', wc_clean( $_POST['billing_ic'] ) );
  38.     }
  39. }

In the first action, we add the fields we want via the built-in Woocommerce function “woocommerce_form_field()”. Select the type (text, radio, select, number,…), set the obligation (in my case both are required) and set the name. Here you need to be careful that the id of the new field (the first parameter of the function) is equal to the real name of the field. So in my case billing_company and billing_ic (billing_ic comes from the wpify plugin). The isset function, which is located in the last parameter, ensures that in case of a failed registration, the written data remains in its fields and the user does not have to write it out again. Since I am adding two fields (company and ic), I also have to use the woocommerce_form_field() function twice. Once for the company and once for the ic.

In the second action we perform the validation. If after submitting the form we find that the ID number or company name is not filled in, we will return the user back with the error “This field is required”. The hook woocommerce_register_post will be executed when the button is clicked.

In the third action, which is performed right after the new user is saved, we still need to update its metadata, namely billing_company and billing_ic. We use the update_user_meta() function, into which we insert the $_POST value. This is exactly the value that the user typed into the form.
 

The result?

As a result, the new fields in the registration form are correctly saved to the user and then dragged into the checkout and the user profile.


 

Leave a Comment

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

Scroll to Top