I want to Programmatically check VAT number is valid in checkout page magento 2.3.X whether using of event or plugin.

Please provide any suggestion to achieve it.


2 Answers 2


Please Follow below steps for validate VAT Number in checkout on selection of shipping country

Note : Magento is checking VAT(tax) Validation only for European countries.

1. Vendorname\Modulename\etc\di.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector">
        <plugin name="Vendorname_Modulename_Commontaxcollector" type="Vendorname\Modulename\Plugin\Tax\Model\Sales\Total\Quote\CommonTaxCollectorPlugin" sortOrder="1" />

2. Vendorname\Modulename\Plugin\Tax\Model\Sales\Total\Quote\CommonTaxCollectorPlugin.php

namespace Vendorname\Modulename\Plugin\Tax\Model\Sales\Total\Quote;

class CommonTaxCollectorPlugin
    public function afterMapItem(\Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector $subject, $result
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $vat = $objectManager->create('Magento\Customer\Model\Vat');
    $Checkout = $objectManager->create('Magento\Checkout\Model\Session');
    $addressInformation = $Checkout->getQuote();
    $scopeConfig = $objectManager->create('Magento\Framework\App\Config\ScopeConfigInterface');
    $countries_list = $scopeConfig->getValue('general/country/eu_countries');
    $country = $addressInformation->getShippingAddress()->getCountryId();
    $VatId = $addressInformation->getShippingAddress()->getVatId();
    if(preg_match("/{$country}/i", $countries_list)) {
            $result1 = $vat->checkVatNumber($country,$VatId);
                // Valid VAT Number
               //Invalid VAT Number
    return $result;
  • 1
    Thanks its working !! you shouldn't use $objectManger :) Commented Sep 11, 2019 at 6:23
  • Were you able to remove vat also, if vat number is valid? Commented Feb 18, 2020 at 7:11
  • you can unset vat from $result variable under valid VAT number condition Commented Feb 18, 2020 at 8:10
  • @DivyeshRana can you please explain? or provide code also? Commented Feb 18, 2020 at 8:18
  • @DivyeshRana can you provide code please ?? Commented Feb 18, 2020 at 9:41

VAT Validation is native in Magento 2.3.x: https://docs.magento.com/m2/ee/user_guide/tax/vat.html

You can find the configurations, Validation, and how to configure VAT ID Validation.

In certain business-to-business transactions, VAT is not assessed. Magento can validate a customer’s VAT ID to ensure that VAT is assessed (or not assessed) properly. See VAT ID Validation: https://docs.magento.com/m2/ee/user_guide/tax/vat-validation.html

VAT ID Validation automatically calculates the required tax for B2B transactions that take place within the European Union (EU), based on the merchant and customer locale. Magento performs VAT ID validation using the web services of the European Commission server.

  • will this work for guest users also? And remove their vat from total, if vat is validated? Commented Apr 1, 2020 at 4:57
  • Yes it works both flows @ArsalanUlHaq
    – Joel Davey
    Commented Jun 19, 2020 at 17:00
  • Yes. THanks it worked! Commented Jun 21, 2020 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.