less than a minute read • Updated 20 days ago
Require a tax ID based on country
How to add JavaScript to your checkout template that dynamically requires a customer's tax ID based on their shipping or billing country — for example, requiring it outside the US.
The following functionality allows you to dynamically require the tax ID depending on the country of the customer.
Add JavaScript
In the Foxy admin (https://admin.foxy.io), go to Settings > Templates and add the code to the Custom footer field (Twig is supported; it is injected before the closing </body> tag):
<script type="text/javascript">
function requireTaxID() {
var country = (FC.json.shipping_address.has_shippable_products) ? FC.json.shipping_address.country : FC.json.billing_address.country;
var tax_required = FC.json.required_fields.indexOf("billing_tax_id");
if (country != "US" && tax_required == -1) {
FC.json.required_fields.push('billing_tax_id');
FC.json.config.template_config.custom_checkout_field_requirements['billing_tax_id'] = "required";
FC.checkout.renderCustomerShipping();
FC.checkout.renderCustomerBilling();
} else if (country == "US" && tax_required > -1) {
FC.json.required_fields.splice(tax_required, 1);
FC.json.config.template_config.custom_checkout_field_requirements['billing_tax_id'] = "optional";
FC.checkout.renderCustomerShipping();
FC.checkout.renderCustomerBilling();
}
}
if (FC.json.context == "checkout") {
FC.client.on("customer-address-change", requireTaxID);
FC.client.on("ready", requireTaxID);
}
</script>