1

I want to override the order of "Total Excluding Tax" and "Total Including Tax" in the order confirmation email so I tried to use preference to override, but its not working. I want to override this

block Magento\Tax\Block\Sales\Order\Tax class.

Can you please suggest me how to override this block?

1 Answer 1

3

You cannot write preference for this block Magento\Tax\Block\Sales\Order\Tax class directly, but you can write preference for the block Magento\Sales\Block\Adminhtml\Order\Totals\Tax class and override it. Below is the code snippet sample.

In the, Vendor/Module/etc/di.xml file <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/c <preference for="Magento\Sales\Block\Adminhtml\Order\Totals\Tax" type="Vendor\Module\Block\Adminhtml\Order\Totals\Tax" /> </config>

In the, Vendor\Module\Block\Adminhtml\Order\Totals\Tax.php file

<?php
namespace Vendor\Module\Block\Adminhtml\Order\Totals;
class Tax extends \Magento\Sales\Block\Adminhtml\Order\Totals\Tax
{

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Tax\Model\Config $taxConfig,
    \Magento\Tax\Helper\Data $taxHelper,
    \Magento\Tax\Model\Calculation $taxCalculation,
    \Magento\Tax\Model\Sales\Order\TaxFactory $taxOrderFactory,
    \Magento\Sales\Helper\Admin $salesAdminHelper,

    array $data = []
)
{
    parent::__construct($context, $taxConfig, $taxHelper, $taxCalculation, $taxOrderFactory, $salesAdminHelper, $data);
}

/**
 * @return \Magento\Tax\Block\Sales\Order\Tax
 */
protected function _initGrandTotal()
{
    // Here you can customize your code
    $store = $this->getStore();
    $parent = $this->getParentBlock();
    $grandototal = $parent->getTotal('grand_total');
    if (!$grandototal || !(double)$this->_source->getGrandTotal()) {
        return $this;
    }

    if ($this->_config->displaySalesTaxWithGrandTotal($store)) {
        $grandtotal = $this->_source->getGrandTotal();
        $baseGrandtotal = $this->_source->getBaseGrandTotal();
        $grandtotalExcl = $grandtotal - $this->_source->getTaxAmount();
        $baseGrandtotalExcl = $baseGrandtotal - $this->_source->getBaseTaxAmount();
        $grandtotalExcl = max($grandtotalExcl, 0);
        $baseGrandtotalExcl = max($baseGrandtotalExcl, 0);
        $totalExcl = new \Magento\Framework\DataObject(
            [
                'code' => 'grand_total',
                'strong' => true,
                'value' => $grandtotalExcl,
                'base_value' => $baseGrandtotalExcl,
                'label' => __('Grand Total (Excl.Tax)'),
            ]
        );
        $totalIncl = new \Magento\Framework\DataObject(
            [
                'code' => 'grand_total_incl',
                'strong' => true,
                'value' => $grandtotal,
                'base_value' => $baseGrandtotal,
                'label' => __('Grand Total (Incl.Tax)'),
            ]
        );
        $parent->addTotal($totalExcl, 'grand_total');
        $this->_addTax('grand_total');
        $parent->addTotal($totalIncl, 'tax');
    }
    return $this;
}

}

Your Answer

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

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