1

I added pdf.xml in custom modules etc folder. But it is not reflecting in Invoice PDF. I have debug and found that in the class \Magento\Sales\Model\Order\Pdf\AbstractPdf, on line no. 676 I get custom row values but those row values gets omitted after the statement $total->setOrder($order)->setSource($source);

protected function insertTotals($page, $source)
    {
        $order = $source->getOrder();
        $totals = $this->_getTotalsList();
        $lineBlock = ['lines' => [], 'height' => 15];
        foreach ($totals as $total) {
            echo "<pre>";print_r($total->getData());// I get custom row values here
            $total->setOrder($order)->setSource($source);//But those get lost here

This is a protected function so I cannot add plugin also.

EDIT

Pdf.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/pdf_file.xsd">
    <totals>
        <total name="custfee">
            <title translate="true">Custom Fee</title>
            <font_size>7</font_size>
            <display_zero>true</display_zero>
            <sort_order>100</sort_order>
        </total>
    </totals>
</config>
0

After debugging the \Magento\Sales\Model\Order\Pdf\AbstractPdf::insertTotals():

$total->setOrder($order)->setSource($source);

On this line, Magento simply sets two Objects data to the current total object for using later. You can get the Order and Source (Invoice object - \Magento\Sales\Model\Order\Invoice) data:

$total->getOrder()->getData(); // Order data
$total->getSource()->getData(); // Invoice data
//Get specific data from total object
$total->getSourceField();
$total->getSortOrder();

enter image description here

If we're trying to var dump or print the total object, the page can be crashed because this object is "heavy" after set order and invoice source object.

I think your problem comes $total->canDisplay() or $total->getTotalsForDisplay().

1) Magento\Sales\Model\Order\Pdf\Total\DefaultTotal::canDisplay(). This method will check:

  • Amount is not equal to zero.
  • Or, in your pdf.xml config, you have declare <display_zero>true</display_zero>. Set to true if you want to display zero amount.

2) Magento\Sales\Model\Order\Pdf\Total\DefaultTotal::getTotalsForDisplay(): Get array of arrays with totals information for displaying in PDF

| improve this answer | |
0

To include a custom total row in pdf, we need to enter the details of the custom row which we need to show in sales/pdf.xml.

For example: Giftwrapping amount will not show in the Invoice Print PDF. To show the Giftwrapping amount in PDF, add the below code in pdf.xml. Copy the pdf.xml (vendor/magento/module-sales/etc/pdf.xml) from the vendor and put in a custom module and add only the below code in the pdf.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/pdf_file.xsd">
    <totals>
        <total name="magento_giftcardaccount_wrapping">
            <title translate="true">Gift Wrapping for Items</title>
            <source_field>gw_items_price</source_field>
            <font_size>7</font_size>
            <display_zero>false</display_zero>
            <sort_order>450</sort_order>
        </total>
    </totals>
</config>

In this way, we can add custom row total in the pdf totals section.

Add the field name from the table which we need to show in the source_field, display_zero is used to show this row total even the value is 0, we can give any name in the name field.

Thanks!!!

| improve this answer | |

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.