Home / Archive / MemberPress: Make VAT field required for Registering EU-based Users
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Make VAT field required for Registering EU-based Users

Adding this code snippet will make the VAT Number field on registration forms required for EU-based users.

For this code snippet to work, the Enable Tax Calculations checkbox needs to be ticked at Dashboard > MemberPress > Settings > Taxes tab. In addition, the Enable VAT and Tax all EU Businesses sub-options need to be enabled also.

The code snippet will add an error message that will be displayed if an EU-based user tries to register without adding a VAT number. The default "Sorry, but you must enter a valid company VAT Number to register." error text can be changed on this line:

$errors[] = 'Sorry, but you must enter a valid company VAT Number to register.';

Code Preview
php
<?php
function mepr_cust_limit_eu_signups( $errors ) {
  $usr_country = sanitize_text_field( $_POST['mepr-address-country'] );
  $countries = require( MEPR_DATA_PATH.'/taxes/vat_countries.php' );
  //If EU country, but not GB, and no vat number entered, throw error
  if( array_key_exists( $usr_country, $countries ) && 'GB' != $usr_country && !isset( $_POST['mepr-vat-number'] ) ) {
    $errors[] = 'Sorry, but you must enter a valid company VAT Number to register.'; //User can edit error message from here
  }
  return $errors;
}
add_filter( 'mepr-validate-signup', 'mepr_cust_limit_eu_signups', 9 );

Comments

Add a Comment