Make VAT number optional for B2C customers
Friday, 04 November 2022
WooCommerce B2B has the option “Make VAT number field required” which allows you to make the VAT number field mandatory or optional.
In some particular scenarios (especially in hybrid B2C / B2B shops), based on your specific needs, you may need to differentiate the behavior (optional / mandatory) based on the customer.
For example, if you want to make VAT number field optional for presumed B2C customers, 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( 'woocommerce_billing_fields', 'wcb2b_vat_number_b2c_optional', 20 ); function wcb2b_vat_number_b2c_optional( $fields ) { $user_group_id = get_the_author_meta( 'wcb2b_group', get_current_user_id() ); if ( $user_group_id == '' || $user_group_id === get_option( 'wcb2b_guest_group', false ) ) { $fields['billing_vat']['required'] = false; } return $fields; }
With this snippet, if the customer is not logged in, or belongs to special GUEST group, or does not belong to any group, it will be considered B2C and the VAT number field will be forced as optional, regardless of WooCommerce B2B “Make VAT number field required” option.
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