Magento2 Configurable Product – Apply Tax Class of Simple Product to Configurable Product Price

configurable-productmagento2priceproducttax

Magento 2 version 2.2.2

Steps to set tax rules:
Create a configurable product, which has two simple (child) products.
Configurable product is set as taxable with 20% tax setup.
First simple product is set as taxable with 20% tax setup.
Second simple product is set as no tax i.e. 0% tax.
Add second product through configurable options to cart.

Actual result:
Tax of 20% is being applied in cart for the second simple product.

Expected result:
0% tax should be applied for the second simple product.

How can I modify this to achieve my requirement?

Best Answer

I was having the same issue due to UK VAT rules and found several magento bug created on github but no actual fix.

So then while looking for a workaround I have built a module to change the Magento behaviour according to our requirement.

Basically we need to update the definition of mapItem method of class Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector

For that we need to override 2 magento classes i.e. write preference in di.xml

<preference for="Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector" type="VendorName\MagentoChildProductTaxExtend\Preference\Tax\Model\Sales\Total\Quote\CommonTaxCollector"/>
<preference for="Magento\Tax\Model\Sales\Total\Quote\Tax" type="VendorName\MagentoChildProductTaxExtend\Preference\Tax\Model\Sales\Total\Quote\Tax"/>

Now in mapItem method of class VendorName\MagentoChildProductTaxExtend\Preference\Tax\Model\Sales\Total\Quote\CommonTaxCollector in line 211, we need to replace the $item->getProduct()->getTaxClassId() with the child product tax class id.

For that, we can change to these lines of code in our own class -

    $taxClassId = $item->getProduct()->getTaxClassId();
    if($item->getHasChildren()) {
        foreach ($item->getChildren() as $child) {
            $taxClassId = $child->getProduct()->getTaxClassId();
        }
    }
    /** @var \Magento\Tax\Api\Data\QuoteDetailsItemInterface $itemDataObject */
    $itemDataObject = $itemDataObjectFactory->create();
    $itemDataObject->setCode($item->getTaxCalculationItemId())
        ->setQuantity($item->getQty())
        ->setTaxClassKey(
            $this->taxClassKeyDataObjectFactory->create()
                ->setType(TaxClassKeyInterface::TYPE_ID)
                //->setValue($item->getProduct()->getTaxClassId())
                ->setValue($taxClassId)
        )
        ->setIsTaxIncluded($priceIncludesTax)
        ->setType(self::ITEM_TYPE_PRODUCT);

Create your own module and add those codes, this works in every tax related section in magento, Tested on Ver 2.2.2

Cheers!!