Conditional VAT Based on a Country Selection (Paid Memberships Pro or any other plugin)

Conditional VAT Based on a Country Selection (Paid Memberships Pro or any other plugin)

Recently I got a job offer and the client needed a custom solution that can be able to handle VAT for a Paid Memberships Pro plugin.

The client’s company is based in Belgium and therefore the tax rate calculation had to include 3 different parameters.

  1. Charge 0% VAT to all customers who insert their VAT number
  2. Charge 21% VAT to all customers based in Belgium with or without VAT number
  3. Charge 21% VAT to all customers without VAT number

This conditional VAT had to be integrated to Paid Memberships Pro registration page.   Before I was officially hired, my client told me that all the developers she talked with said pretty much the same thing.  “This is a complex project, 2 months of work minimum” etc.. etc..

The developer has to be able to think outside-the-box.  And instead to integrate this conditional VAT directly to PMP which was suggested by all other dev’s, I came up with a solution within 2 hours, not 2 months.

And the best part is that this solution can be used with any plugin and if needed you can modify it a little bit and use for other things, not just for conditional VAT.

Simplicity is the Key to Success

You have a PMP (or any other membership plugin) default registration page that cannot be easily modified.

Instead to send your visitors that reg. page, send them to the custom WordPress page that will serve as a crossroad and direct visitors to certain location.

A visitor from US goes here, the other one from Japan to some other page etc…

Code for Custom-Page.php (replace preformatted quotation marks)

<form action = "https://yoursite.com/wp-admin/admin-post.php" method="post">

<input name="action" type="hidden" value="vat_tax" /><label for="bcountry">Country</label>

<select name="bcountry" data-region-id="bstate" data-value="shortcode" data-crs-loaded="true">
<option value="">Select country</option>
<option value="AS">American Samoa</option>
<option value="BE">Belgium</option>
</select>

<label for="tax">VAT Number</label>
<input id="vat" name="vat" type="text" />

<input type="submit" value="Next" />

</form>

Now open up your theme functions.php file and drop this code at the bottom:

add_action( 'admin_post_vat_tax', 'vat_tax_process' );

function vat_tax_process() 
{
/* here you must put your code */

$taxvat= $_POST['vat'];

if ($_POST['bcountry'] != 'BE' && (strlen($taxvat) >= 3)
) {

header('location: https://yoursite.com/membership-account/membership-levels/');

}

elseif

($_POST['bcountry'] === 'BE') {

header('location: https://yoursite.com/membership-account/membership-levels-vat'); 

}

else

if (empty($taxvat)) {
header('location: https://yoursite.com/membership-account/membership-levels-vat');

}

var_dump($_REQUEST['action']);
exit();
}

As you can see in the code, everything is nicely ordered.  If customer enters their VAT number, redirect to membership level where VAT is excluded.  If customer is from Belgium, redirect to memb. level where VAT is included within the price.

And in the end there’s an option to redirect customers who didn’t entered their VAT number to level where VAT tax is included.

This solution do not have a VAT validation, but the validation can be added with a plugin as a next step during registration (which is already done on my client’s website).