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

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.