Вопрос

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.

Это было полезно?

Решение

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" />
    </type>
</config>

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)) {
        if($VatId){
            $result1 = $vat->checkVatNumber($country,$VatId);
            if($result1->getIsValid()){
                // Valid VAT Number
            }else{
               //Invalid VAT Number
            }
        }
    }
    return $result;
    }
}

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top