Add shipping fields to customer extended registration form

Saturday, 28 August 2021

WooCommerce B2B among its many features has the possibility to extend the customer registration form by entering the billing address fields, simply enable the “Extended registration form” option in the plugin settings page.
WooCommerce B2B has a dedicated hook to manage and customize this form!
To expand this form even further and also enter the shipping address fields, edit functions.php file that you find in your site’s FTP space in:

 wp-content/themes/{YOUR_ACTIVE_THEME}/functions.php

and insert this code:

add_filter( 'wcb2b_register_form_fields', 'fn_wcb2b_register_form_fields' );
function fn_wcb2b_register_form_fields( $fields ) {
    $checkout = WC_Checkout::instance();
    $shipping_fields = $checkout->get_checkout_fields( 'shipping' );
    $fields = array_merge( $fields, $shipping_fields);
    return $fields;
}

The fields of the billing and shipping addresses may have the same labels, creating confusion for the customer during registration. I therefore suggest that you customize the labels, so as to differentiate them from each other, for example:

add_filter( 'wcb2b_register_form_fields', 'fn_wcb2b_register_form_fields' );
function fn_wcb2b_register_form_fields( $fields ) {
    $checkout = WC_Checkout::instance();
    $shipping_fields = $checkout->get_checkout_fields( 'shipping' );
    $fields = array_merge( $fields, $shipping_fields);
    // Change label to prevent confusion
    $fields['shipping_first_name']['label'] = __( 'Shipping first name', 'woocommerce-b2b' );
    return $fields;
}

It will transform the default label of the shipping address name “First name” into “Shipping first name”.

WARNING! Before making any changes, make sure you have made a complete backup of site files and database.
It is advisable not to edit the theme files directly, but to use a child theme.
For more information on child themes, please read: WordPress Official guide on Child Themes