Magento Import Tax Rates. The Full Guide

2 min read

Send to you:

Magento® 2 provides the calculation of the tax amount when creating an order. Magento firstly was developed for the USA, because sales tax rates can be different in different locations (county/city). The default Magento 2 functionality allows setting the tax rate by the following parameters: country, region (state/province), postcode. The administrator can set the tax rates in Stores -> Tax Zones, and Rates menu on admin panel. But as the USA has a lot of locations with different postcodes, it could be a bit challenging to output all necessary tax rates pegged to the postcode manually.

Now we’ll consider the way of how to do this.

How In Magento Set Tax Rate Programmatically

Let’s imagine the CSV-file of Magento tax class for shipping rates (more than 1000 rows) for Northern Carolina state that has the following format:

State,ZipCode,TaxRegionName,StateRate,EstimatedCombinedRate,EstimatedCountyRate,EstimatedCityRate,EstimatedSpecialRate,RiskLevel

We’ll pay attention to a ZipCode field which contains the location postcode and Estimated Combined Rate presented in the decimal format.

$api_url = "….";//link to Magento 2 Store
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array('username'=>'admin','password'=>'...')));
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type:application/json"));
curl_setopt($ch,CURLOPT_URL,$api_url.'rest/V1/integration/admin/token/');
$res = curl_exec($ch);
$result = json_decode($res,true);
if(isset($result['message'])){
  echo $result['message'];
  exit(0);
}
$token = $result;//Getting the connection token 
$file = fopen('TAXRATES_ZIP5_NC201701.csv','rt');
$header = fgetcsv($file);
while($row = fgetcsv($file)){
   $taxArray = array(
     'tax_country_id'=>'US',
     'tax_region_id'=>44,
     'tax_postcode'=>$row[1],
     'code'=>'US-NC-'.$row[1],
     'rate'=>$row[4]*100
   );
    curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array('taxRate'=>$taxArray)));
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-type: application/json","Authorization: Bearer ".$token));
curl_setopt($ch,CURLOPT_URL,$api_url.'rest/V1/taxRates');
$res = curl_exec($ch);
echo $res."<br/>";   
}
fclose($file);

This example describes the tax rate to percent value because the tax rates are set exactly in this format in Magento 2. As we needed to place the configuration of the tax rates only for Northern Carolina, it was not difficult to find the identifier of this state in Magento based online store. We created an entry in tax_calculation_rate table for each location by requesting _/V1/taxRates() API method and transmitting it the array in JSON format as a parameter.

If the file provided the data for locations in different states, we would better get the array of states using_/V1/directory/countries/US() API method which would return the array with information about the USA from Magento 2. This array would include state id – state code binding. First, we would get the state code, and then we would place the state id in the request for the appropriate state code from State column.

SUM MARY

The main advantage of this method is that the script could have no clue about Magento, except the accesses to the admin panel, the website link, and API methods which it requests. That’s why this script can run on the host that is different from Magento’s host. We hope that our quick tip will help you to get along with Magento tax. If you have other questions related to Magento 2 Development please contact us.

Posted on: May 08, 2017

4.0/5.0

Article rating (4 Reviews)

Do you find this article useful? Please, let us know your opinion and rate the post!

  • Not bad
  • Good
  • Very Good
  • Great
  • Awesome