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:
- make Company Name and Company ID a required field
- 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:
add_filter( 'woocommerce_billing_fields', 'ts_require_wc_company_field');
function ts_require_wc_company_field( $fields ) {
$fields['billing_company']['required'] = true;
$fields['billing_ic']['required'] = true;
return $fields;
}
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.
add_action( 'woocommerce_register_form', 'misha_add_register_form_field' );
function misha_add_register_form_field(){
woocommerce_form_field(
'billing_company',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'Názov firmy'
),
( isset($_POST['billing_company']) ? $_POST['billing_company'] : '' )
);
woocommerce_form_field(
'billing_ic',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'IČO'
),
( isset($_POST['billing_ic']) ? $_POST['billing_ic'] : '' )
);
}
add_action( 'woocommerce_register_post', 'misha_validate_fields', 10, 3 );
function misha_validate_fields( $username, $email, $errors ) {
if ( empty( $_POST['billing_company'] ) ) {
$errors->add( 'billing_company', 'Toto pole je povinné' );
}
if ( empty( $_POST['billing_ic'] ) ) {
$errors->add( 'billing_ic', 'Toto pole je povinné' );
}
}
add_action( 'woocommerce_created_customer', 'misha_save_register_fields' );
function misha_save_register_fields( $customer_id ){
if ( isset( $_POST['billing_company'] ) ) {
update_user_meta( $customer_id, 'billing_company', wc_clean( $_POST['billing_company'] ) );
update_user_meta( $customer_id, 'billing_ic', wc_clean( $_POST['billing_ic'] ) );
}
}
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.
I have been programming websites since 2006. I have worked in several smaller and larger companies, always as a developer. I know how to program in PHP and lately I have been exclusively working on small and medium-sized projects in WordPress.