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;
}
}
Best Answer
I have also tried with preference but it did not work so have done using the plugin. Please refer below files