Skip to content

Instantly share code, notes, and snippets.

@rynaldos-zz
Last active February 3, 2017 08:37
Show Gist options
  • Save rynaldos-zz/75c93a05fcb12ef179a8 to your computer and use it in GitHub Desktop.
Save rynaldos-zz/75c93a05fcb12ef179a8 to your computer and use it in GitHub Desktop.
[WooCommerce] Make the VAT field required for select countries
add_action('woocommerce_checkout_process', 'wc_force_vat_field_to_be_not_empty');
function wc_force_vat_field_to_be_not_empty() {
global $woocommerce;
// Define countries where VAT field is mandatory
$eu_countries = array('AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SI', 'SK');
// Check if customer is in one of those countries
if( in_array( $woocommerce->customer->get_country(), $eu_countries ) ) {
// Check if VAT field isn't empty
if ( ! $_POST['vat_number'] )
// Create notice
wc_add_notice( __( 'Please enter your VAT number.' ), 'error' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment