less than a minute read • Updated 9 minutes ago
Require a tax ID based on country
How to require the tax ID depending on the customer's country.
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):
javascript
<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>