1

I want to override a model call (Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector).

We used preference to override a model call inside di.XML like magento 2.1.x But it is not working on Magento 2.2

etc/di.xml

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

</config>

app\code\MyModule\CustomShippingTax\Model\Sales\Total\Quote\CommonTaxCollector.php

 <?php  

  namespace MyModule\CustomShippingTax\Model\Sales\Total\Quote;
    /**
     * Tax totals calculation model
     * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
     */
    class CommonTaxCollector extends \Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector
    {

        public function mapAddress(QuoteAddress $address)
        {
            $customerAddress = $this->customerAddressFactory->create();
            $customerAddress->setCountryId($address->getCountryId());
            $customerAddress->setRegion(
                $this->customerAddressRegionFactory->create()->setRegionId($address->getRegionId())
            );
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();


    var_dump($address->getPostcode());
    exit;

            $customerAddress->setCity($address->getCity());
            $customerAddress->setStreet($address->getStreet());

            return $customerAddress;
        }


    }

Please, can you help me on above issue?Any reference and suggestion is highly appreciated.

Thanks

6
  • what error you are getting? Jan 19 '18 at 16:58
  • @AbhishekPanchal no error nothing. Jan 19 '18 at 17:12
  • Try my code from answer. Jan 19 '18 at 17:13
  • @AbhishekPanchal thanks for the answer I have tried but not working. Jan 19 '18 at 17:18
  • Can you please try my code again with both the files. Jan 19 '18 at 17:30
3

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

app/code/[VendorName]/[ModuleName]/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\Tax" type="[VendorName]\[ModuleName]\Model\Sales\Total\Quote\Tax" />
    <preference for="Magento\Tax\Model\Sales\Total\Quote\Subtotal" type="[VendorName]\[ModuleName]\Model\Sales\Total\Quote\Subtotal" />
    <preference for="Magento\Tax\Model\Sales\Total\Quote\Shipping" type="[VendorName]\[ModuleName]\Model\Sales\Total\Quote\Shipping" />
</config>

OR

Try following code:

  1. app/code/[VendorName]/[ModuleName]/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">
    <type name="Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector">
        <plugin name="[vendorname]_[modulename]_commontaxcollector" type="[VendorName]\[ModuleName]\Plugin\CommonTaxCollectorPlugin" sortOrder="1" />
    </type>
</config>
  1. app/code/[VendorName]/[ModuleName]/Plugin/CommonTaxCollectorPlugin.php
<?php
namespace [VendorName]\[ModuleName]\Plugin;

class CommonTaxCollectorPlugin
{
    public function afterMapAddress(
        \Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector $subject,
        \Magento\Customer\Model\Data\Address $customerAddress
    ) {
        var_dump($customerAddress->getPostcode());die;
    }
}

Note: Output of var_dump is display in console, check action estimate-shipping-methods-by-address-id

5
  • Thanks!!! It's Working.But why preference is not working? Jan 19 '18 at 19:03
  • @KamleshJha - Review updated answer.
    – Pratik Oza
    Jan 19 '18 at 19:09
  • Hi Pratik, can you explain please what is dependency injection in context of magento 2. how can i check that dependency injection is not working on particular class Jan 19 '18 at 19:49
  • @PratikOza Not working for me. I tried both way. Any idea why ? Jun 11 '18 at 11:43
  • I am using Plugin way, public function aroundMapItem( \Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector $subject, \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $itemDataObjectFactory, AbstractItem $item, $priceIncludesTax, $useBaseCurrency, $parentCode = null ) { //My Content } Function is calling but not working. Jun 11 '18 at 14:00

Your Answer

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

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