1

I am trying to override mapItem method from "Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector" using below code. However code written in my local module's class is not invoked.

VendorName\Tax\Model\Sales\Total\Quote\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector" type="VendorName\Tax\Model\Sales\Total\Quote\CommonTaxCollector" />
</config>

VendorName\Tax\Model\Sales\Total\Quote\CommonTaxCollector.php

<?php


namespace VendorName\Tax\Model\Sales\Total\Quote;

use Magento\Quote\Model\Quote\Item\AbstractItem;

Class CommonTaxCollector extends \Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector {

/**
 * Map an item to item data object
 *
 * @param \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $itemDataObjectFactory
 * @param AbstractItem $item
 * @param bool $priceIncludesTax
 * @param bool $useBaseCurrency
 * @param string $parentCode
 * @return \Magento\Tax\Api\Data\QuoteDetailsItemInterface
 */
public function mapItem(
    \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $itemDataObjectFactory,
    AbstractItem $item,
    $priceIncludesTax,
    $useBaseCurrency,
    $parentCode = null
) {
    $objmanager = \Magento\Framework\App\ObjectManager::getInstance();
    $logger = $objmanager->get("Psr\Log\LoggerInterface");
    $logger->debug("Testing if local method is called");

    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());
        }
        $itemDataObject->setUnitPrice($item->getBaseTaxCalculationPrice())
            ->setDiscountAmount($item->getBaseDiscountAmount());
    } else {
        if (!$item->getTaxCalculationPrice()) {
            $item->setTaxCalculationPrice($item->getCalculationPriceOriginal());
        }
        $itemDataObject->setUnitPrice($item->getTaxCalculationPrice())
            ->setDiscountAmount($item->getDiscountAmount());
    }

    $itemDataObject->setParentCode($parentCode);

    return $itemDataObject;
}

}
1
  • I also tried clearing generation folder along with cache but no luck. Aug 3, 2017 at 18:49

1 Answer 1

0

Since dependency injection did not work on CommonTaxCollector class, alternatively I had to override mapItem method using dependency injection on other files (mentioned below in di.xml) which are extending the class : Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Tax\Model\Sales\Total\Quote\Tax" type="VendorName\Tax\Model\Sales\Total\Quote\Tax" />
    <preference for="Magento\Tax\Model\Sales\Total\Quote\Subtotal" type="VendorName\Tax\Model\Sales\Total\Quote\Subtotal" />
</config>

However it would be great if someone can suggest a way to override Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector directly.

1
  • Can you explain the full process ? It's not working for me. Jun 12, 2018 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.