Home » Blogs » Magento 2 Tax Exempt by Tax ClassId

Magento 2 Tax Exempt by Tax ClassId

Tax classes can be assigned to customers, products, and shipping. Magento analyzes the shopping cart of each customer and calculates the appropriate tax according to the class of the customer, the class of the products in the cart, and the region (as determined by the customer’s shipping address, billing address or shipping origin). New tax classes can be created when a tax rule is defined.

1. Customer — You can create as many customer tax classes as you need, and assign them to customer groups. For example, in some jurisdictions, wholesale transactions are not taxed, but retail transactions are. You can associate members of the Wholesale Customer group with the Wholesale tax class.

2. Product — Product classes are used in calculations to determine the correct tax rate is applied in the shopping cart. When you create product, it is assigned to a specific tax class. For example, food might not be taxed, or be taxed at a different rate.

3. Shipping — If your store charges an additional tax on shipping, you should designate a specific product tax class for shipping. Then in the configuration, specify it as the tax class that is used for shipping.
Magento Tax calculation is based on Tax Classes, The tax class that is used for shipping, and the default tax classes for products and customers are set in the Sales configuration.

To configure tax classes:

  1. On the Admin sidebar, click Stores.
  2. Under Settings, choose Configuration.
  3. In the panel on the left under Sales, choose Tax.
  4. Expand the Tax Classes section. Then, choose the tax class for each of the following:
    • Set Tax Class for Shipping
    • Default Tax Class for Product
    • Default Tax Class for Customer
  5. When complete, click Save Config.

Taxes are applied to products based on assigned tax classes. Tax classes are combined with tax zones and rates to create tax rules. Tax rules are, what are applied to each product or shopping cart to determine the amount of tax charges in a transaction.
Tax module Class Magento\Tax\Model\TaxClass\Management and that method getTaxClassId() is a responsible to return Tax ClassId. We can put our custom logic/trick within getTaxClassId code and applied zero tax.
class Management extends \Magento\Tax\Model\TaxClass\Management
public function getTaxClassId($taxClassKey, $taxClassType = self::TYPE_PRODUCT)
For Example If you have created a Unique Product where you want to apply Zero tax then we can add our custom logic for that product in this method.
For Example If you want to apply Zero tax for a Unique Product then we can easily add custom logic getTaxClassId method. Just you need to create a new Tax class where that tax rate will be Zero.

Example of code:

public function getTaxClassId($taxClassKey, $taxClassType = self::TYPE_PRODUCT)
    {
        if (!empty($taxClassKey)) {
//Do something here and get Prodcut sku ($getSku) and Tax classId ($zeroTaxClassId)
//Assuming,
//Product sku ($getSku) is “unqzerotax” and
//Zero Tax ClassId ($zeroTaxClassId) is “10”
			if ($getSku == "unqzerotax") {
				return $zeroTaxClassId;
			}
			switch ($taxClassKey->getType()) {
                case TaxClassKeyInterface::TYPE_ID:
                    return $taxClassKey->getValue();
                case TaxClassKeyInterface::TYPE_NAME:
                    $searchCriteria = $this->searchCriteriaBuilder->addFilters(
                        [$this->filterBuilder->setField(ClassModel::KEY_TYPE)->setValue($taxClassType)->create()]
                    )->addFilters(
                        [
                            $this->filterBuilder->setField(ClassModel::KEY_NAME)
                                ->setValue($taxClassKey->getValue())
                                ->create(),
                        ]
                    )->create();
                    $taxClasses = $this->classRepository->getList($searchCriteria)->getItems();
                    $taxClass = array_shift($taxClasses);
                    return (null == $taxClass) ? null : $taxClass->getClassId();
            }
        }
        return null;
    }

Need to create a plugin for mapItem method where you will set product sku in $itemDataObject which will get in getTaxClassId method for custom logic.

References: –

https://docs.magento.com

Leave A Comment