0

I am trying to override getFullTaxInfo() of class Magento\Sales\Model\Order\Pdf\Total\DefaultTotal using preference but this is not working.

Can anyone help to do it?

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\Sales\Model\Order\Pdf\Total\DefaultTotal" type="Vendor\Module_name\Model\Rewrite\Sales\Order\Pdf\Total\DefaultTotal" />
</config>

Model File: Vendor/Module_name/Model/Rewrite/Sales/Order/Pdf/Total/DefaultTotal.php

<?php

namespace Vendor\Module_name\Model\Rewrite\Sales\Order\Pdf\Total;


class DefaultTotal extends \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal
{
    public function getFullTaxInfo()
    {
        $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;
        $taxClassAmount = $this->_taxHelper->getCalculatedTaxes($this->getSource());
        if (!empty($taxClassAmount)) {
            foreach ($taxClassAmount as &$tax) {
                $percent = $tax['percent'] ? ' (' . ($tax['percent']-5) . '%)' : '';
                $amount     = ($tax['tax_amount'] / 35) * 30;
                $tax['amount'] = $this->getAmountPrefix() . $this->getOrder()->formatPriceTxt($amount);
                $tax['label'] = __('Sales Taxes') . $percent . ':';
                $tax['font_size'] = $fontSize;
            }
        } else {
            /** @var $orders \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\Collection */
            $orders = $this->_taxOrdersFactory->create();
            $rates = $orders->loadByOrder($this->getOrder())->toArray();
            $fullInfo = $this->_taxCalculation->reproduceProcess($rates['items']);
            $tax_info = [];

            if ($fullInfo) {
                foreach ($fullInfo as $info) {
                    if (isset($info['hidden']) && $info['hidden']) {
                        continue;
                    }

                    $_amount = $info['amount'];

                    foreach ($info['rates'] as $rate) {
                        $percent = $rate['percent'] ? ' (' . $rate['percent'] . '%)' : '';

                        $tax_info[] = [
                            'amount' => $this->getAmountPrefix() . $this->getOrder()->formatPriceTxt($_amount),
                            'label' => __($rate['title']) . $percent . ':',
                            'font_size' => $fontSize,
                        ];
                    }
                }
            }
            $taxClassAmount = $tax_info;
        }

        return $taxClassAmount;
    }
}

    

3 Answers 3

2

I have also tried with preference but it did not work so have done using the plugin. Please refer below files

app/code/Vendor/Module/etc/adminhtml/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\Sales\Model\Order\Pdf\Total\DefaultTotal">
        <plugin name="plugin_name" type="Vendor\Module\Plugin\Model\Order\Pdf\Total\DefaultTotal" sortOrder="10" />
    </type>
</config>

app/code/Vendor/Module/Plugin/Model/Order/Pdf/Total/DefaultTotal.php

<?php
    
namespace Vendor\Module\Plugin\Model\Order\Pdf\Total;

class DefaultTotal
{
    protected $_storeManager;

    public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
    {
        $this->_storeManager = $storeManager;
    }

    public function afterGetFullTaxInfo(
        \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal $subject,
        $result
    ) {    
        foreach ($result as &$tax) {
                $percent = $tax['percent'] ? ' (' . (float)$tax['percent'] . '%)' : '';
                $tax['label'] = __($tax['title']) . $percent . ':';
                
            }
        return $result; 
    }
}
1
  • Thank you @juber sherasiya, it's working for me Nov 13, 2021 at 12:12
1

Your problem here is that the original class that calls getFullTaxInfo() extend explicitely Magento\Sales\Model\Order\Pdf\Total\DefaultTotal (see the code below) so it does not load your overrided class.

Since the function you are trying to override is a public function you should try to use a plugin on this function instead. It is a good practice to always use plugin instead of preference when it is possible.

If for some reasons you can't use a plugin you can try to override the Magento\Tax\Model\Sales\Pdf\GrandTotal class to change the extended class to your custom class with the override.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Tax\Model\Sales\Pdf;

class Grandtotal extends \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal
1
  • Thank you @Carlo. I have done using the plugin. I am sharing my plugin in answer Sep 8, 2021 at 8:51
0

I have done using the plugin. I override the array for tax information.

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="magento:framework:ObjectManager/etc/config.xsd">
        <plugin disabled="false" name="Vendor_Module_Plugin_Magento_Sales_Model_Order_Pdf_Total_DefaultTotal" sortOrder="10" type="Vendor\Module\Plugin\Magento\Sales\Model\Order\Pdf\Total\DefaultTotal"/>
</config>

Plugin File: Vendor/Module_name/Plugin/Magento/Sales/Model/Order/Pdf/Total/DefaultTotal

<?php
    
namespace Vendor\Module_name\Plugin\Magento\Sales\Model\Order\Pdf\Total;

class DefaultTotal
{
    protected $_storeManager;

    public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
    {
        $this->_storeManager = $storeManager;
    }

    public function afterGetFullTaxInfo(
        \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal $subject,
        $result
    ) {
        // return $result;
        $currency = $this->_storeManager->getStore()->getCurrentCurrency()->getCode();
        $result1 = array();
        $result1[0]['title'] = 'Sales Tax';
        $result1[0]['percent'] = $result[0]['percent']-5;
        $result1[0]['tax_amount'] = number_format(($result[0]['tax_amount']/35)*30,2);
        $result1[0]['base_tax_amount'] = number_format(($result[0]['base_tax_amount']/35)*30,2);
        $result1[0]['amount'] = $currency . number_format(($result[0]['tax_amount']/35)*30,2);
        $result1[0]['label'] = 'Sales Tax ('.number_format($result[0]['percent']-5,2).'%)';
        $result1[0]['font_size'] = 10;
        return $result1;
    }
}

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.