How to Import Tax Rates in Magento 2

WEB4PRO
WEB4PRO
Jul 25, 2017 · 3 min read

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 onadmin 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.


Importing Tax Rates with Magento 2 Web API

Let’s imagine the CSV-file of tax 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 to the configuration 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.

So, that’s all :) We are looking forward to your questions and communication with you. And we hope this quick tip will help you get along with tax rates in Magento 2.

You can find more useful tips on Magento and Magento 2 on our website.

Originally published at web4pro.net.

WEB4PRO

It’s all about eCommerce development: How-To Tutorials, Essential Guides, Platform Comparison, etc.

WEB4PRO

Written by

WEB4PRO

eCommerce Engineering Company

WEB4PRO

WEB4PRO

It’s all about eCommerce development: How-To Tutorials, Essential Guides, Platform Comparison, etc.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade