5

I have checked below mentioned files to override tax functionality:

  • \vendor\magento\module-tax\Model\Sales\Total\Quote\Tax.php

  • \vendor\magento\module-tax\Model\Sales\Total\Quote\CommonTaxCollector.php

I am looking into the mapItem function in CommonTaxCollector.php. I have added static values for setBaseTaxCalculationPrice and setTaxCalculationPrice, but cart row total, subtotal and tax are changed.

I want to change only tax to apply it on manufacturer cost instead of price. Row total and Total should not be affected.

public function mapItem(
    \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $itemDataObjectFactory,
    AbstractItem $item,
    $priceIncludesTax,
    $useBaseCurrency,
    $parentCode = null
) {
    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/mylogfile3.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info($item->getTaxCalculationPrice());

    if (!$item->getTaxCalculationItemId()) {
        $sequence = 'sequence-' . $this->getNextIncrement();
        $item->setTaxCalculationItemId($sequence);
    }
    /** @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())
        )
        ->setIsTaxIncluded($priceIncludesTax)
        ->setType(self::ITEM_TYPE_PRODUCT);

    if ($useBaseCurrency) {
        if (!$item->getBaseTaxCalculationPrice()) {
            //$item->setBaseTaxCalculationPrice($item->getBaseCalculationPriceOriginal());

            $item->setBaseTaxCalculationPrice('200');
        }

        $itemDataObject->setUnitPrice($item->getBaseCalculationPriceOriginal())
            ->setDiscountAmount($item->getBaseDiscountAmount());
    } else {
        if (!$item->getTaxCalculationPrice()) {
            //$item->setTaxCalculationPrice($item->getCalculationPriceOriginal());

            $item->setTaxCalculationPrice('200');
        }

        $itemDataObject->setUnitPrice($item->getBaseCalculationPriceOriginal())
            ->setDiscountAmount($item->getDiscountAmount());
    }

    $itemDataObject->setParentCode($parentCode);
    return $itemDataObject;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.