1

I need to display the grand total base currency value on invoices that have been generated using other currencies than base. And want to display all the values on the PDF invoice could be displayed in both currencies. Please see attached image for better understanding.

enter image description here

Thank you.

3
  • Any solution please ? i need to print invoice with the 2 currencies
    – Aba
    Commented May 29, 2020 at 0:57
  • yes. i created event for it. event name is sales_order_place_after. and save value of currency that we need to display. and then do calculate with the base currency
    – Ronak
    Commented Jun 3, 2020 at 13:52
  • @Ronak Can you tell me how you did it ? Can you share that code in reply. Thanks Commented Aug 17, 2021 at 4:42

1 Answer 1

1

All the data you need is already in the order object and Magento allows you to easily add to the various order totals in your own module using a pdf.xml config files.

Create a new module in the usual way. In that module add an etx/pdf.xml config file with the contents:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/pdf_file.xsd">
    <totals>
        <total name="base_grand_total">
            <title translate="true">Base Grand Total</title>
            <source_field>base_grand_total</source_field>
            <font_size>8</font_size>
            <display_zero>true</display_zero>
            <sort_order>800</sort_order>
            <model><My Namespace>\<My Module>\Model\Order\Pdf\BaseGrandTotal</model>
        </total>
    </totals>
</config>

The title, is what will show up on the PDF invoice. The source_field, is the database column name you want to display, which, as you want to display the base currency grand total, should be 'base_grand_total'.

The sort order sets the relative position of your total in the last section of the invoice. As of M2.3.5, the grand total has a sort order of 700, so setting this to 800 places it at the end of the invoice. If you search the code base for pdf.xml, you'll find all the other config files which get merged together. Take a look at the total nodes within the totals node and that will let you see which totals will be included and their relative position by sort order.

If you ran the above without including the model node, you'd get the figure you were after, but with a currency symbol that matched the order currency. This is because the Magento\Sales\Model\Order\Pdf\Total\DefaultTotal class uses the orders formatPriceTxt method, which in turn uses the order currency for the formatting, not the base currency. To handle this, you need the model node above to point your own subclass of DefaultTotal, where you can modify the behaviour to use the the base currency for the formatting.

Add the class, referenced above in your pdf.xml to your module. In the file <My Namespace>/<My Module>/Model/Order/Pdf/BaseGrandTotal.php add the content:

namespace <My Namespace>\<My Module>\Model\Order\Pdf;

class BaseGrandTotal extends \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal
{
    public function getTotalsForDisplay()
    {
        $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;

        $amount = $this->getOrder()->getBaseCurrency()->formatTxt($this->getAmount());

        $totals = [
            [
                'amount' => $this->getAmountPrefix() . $amount,
                'label' => __($this->getTitle()) . ':',
                'font_size' => $fontSize,
            ],
        ];

        return $totals;
    }
}

This formats the amount using the base currency of the order, not the order currency.

1
  • Yeah, it's working fine. and For subtotal and shipping how to i can display in base currency ? Commented Aug 16, 2021 at 18:15

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.