When a customer creates an account for your Magento 2 store, it is necessary to enter their address so they can use the saved address for future orders. During tax implementation, the admin requires the customer’s region information. Magento 2 maintains a database that allows customers to select their country, state, and region.
Taxes can be calculated based on the customer’s address region. For example, if a customer is in California, New York, or Florida, you need to set the specific tax value for that region.
In this guide, we’ll take you through the step-by-step process of how to set up a custom tax for a specific country, state, and region in Magento 2.
ON THIS PAGE
Steps to Create a Custom Tax for a Specific Region in Magento 2
Creating a custom tax for a specific region involves setting up a unique tax rate or rule tailored to that area. This allows businesses to adhere to specific tax regulations or requirements applicable to a particular region, such as a state, province, or country.
To create a custom tax for a specific region, follow the steps below:
Step 1: Create a “registration.php”
app/code/Dckap/AlphanumericTax/registration.php
<?php
Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Dckap_AlphanumericTax',
__DIR__
);
Step 2: Create a “Module.xml” file and the file path:
app/code/Dckap/AlphanumericTax/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Dckap_AlphanumericTax" setup_version="1.0.3">
<sequence>
<module name="Magento_Tax"/>
</sequence>
</module>
</config>
Step 3: Create one more file “sales.xml”
Now follow this file path:
app/code/Dckap/AlphanumericTax/etc/sales.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
<section name="quote">
<group name="totals">
<item name="customtax" instance="Dckap\AlphanumericTax\Model\Total\Quote\CustomTax" sort_order="500"/>
</group>
</section>
</config>
Step 4: Create “CustomTax.php”
app/code/Dckap/AlphanumericTax/Total/Quote/CustomTax.php
<?php
namespace Dckap\AlphanumericTax\Model\Total\Quote;
use Exception;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Address\Total;
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
use Magento\Tax\Model\Calculation;
/**
* Class CustomTax
* @package Dckap\AlphanumericTax\Model\Total\Quote
*/
class CustomTax extends AbstractTotal
{
/**
* @var PriceCurrencyInterface
*/
protected $priceCurrency;
/**
* @var Calculation
*/
protected $calculation;
/**
* @var CustomerSession
*/
protected $customerSession;
/**
* @var \Magento\Framework\DB\Adapter\AdapterInterface
*/
protected $connection;
/**
* CustomTax constructor.
* @param \Magento\Framework\App\ResourceConnection $resource
* @param PriceCurrencyInterface $priceCurrency
* @param Calculation $calculation
* @param CustomerSession $customerSession
*/
public function __construct(
\Magento\Framework\App\ResourceConnection $resource,
PriceCurrencyInterface $priceCurrency,
Calculation $calculation,
CustomerSession $customerSession
)
{
$this->priceCurrency = $priceCurrency;
$this->calculation = $calculation;
$this->customerSession = $customerSession;
$this->connection = $resource->getConnection();
}
/**
* Collect extra fee totals
*
* @param Quote $quote
* @param ShippingAssignmentInterface $shippingAssignment
* @param Total $total
*
* @return $this
* @throws Exception
*/
public function collect(
Quote $quote,
ShippingAssignmentInterface $shippingAssignment,
Total $total
)
{
parent::collect($quote, $shippingAssignment, $total);
$totalTax = $this->getProductInfor($quote);
$total->setTaxAmount($totalTax); //set tax amount here
$total->setGrandTotal($total->getBaseGrandTotal() + $totalTax);
return $this;
}
/**
* @param $quote
* @return int|string
* @throws \Zend_Log_Exception
*/
public function getProductInfor($quote)
{
if ($this->customerSession->getCustomer()) {
//Added your logic here
$customer = $this->customerSession->getCustomer();
$defaultShippingAddress = $customer->getDefaultShippingAddress();
$taxPercent = "";
if ($defaultShippingAddress) {
$region = $defaultShippingAddress->getRegion();
if ($region == 'California') {
$taxPercent = 5;
} elseif ($region == 'Florida') {
$taxPercent = 10;
} elseif ($region == 'New York') {
$taxPercent = 15;
}
}
return $taxPercent;
}
}
}
Read Also: Create a Custom Email in Magento 2
Custom Tax Usage
- Identify the region like determine the specific region you need to create the custom tax. This could be a state, province, country, or even a smaller jurisdiction within a larger area.
- Research tax regulations like requirements for the identified region. Understand the applicable tax rates, rules, and exemptions to consider when creating the custom tax.
- Access tax settings to the configuration section of your platform or software. This could be within your ecommerce platform, accounting software, or tax management system.
- Set conditions to specify any conditions or criteria when the custom tax should be applied. This includes factors like product categories, customer groups, or order totals.
- Test the tax before applying the custom tax rule live, test it thoroughly to ensure it calculates taxes accurately and applies only when intended.
- Once you are satisfied with the setup and testing, proceed to implement the custom tax rule for the specific region in your system.
Specific Region’s Output:
Region for California
Region for New York
Conclusion
Creating a custom tax for a specific region is essential for businesses to comply with tax regulations and requirements customized to that area. By understanding the tax regulations, setting up accurate tax rules, and implementing them effectively, businesses can ensure compliance and accurate tax calculations for transactions within the designated region. This helps adhere to legal requirements and contributes to a smoother and more transparent tax management process.