3

Magento 2.3.2

In my invoice PDF if Shipping & Handling amount is null or 0 it hides that row like this:

enter image description here

But I want to to display it as $0.00 if the price is null like this:

enter image description here

I found this file /vendor/magento/module-sales/Model/Order/Pdf/Total/DefaultTotal.php, in this file there is a function canDisplay() which is responsible for hiding the Title and Amount from Total section in invoice PDF. so I did some changes in function to display Shipping and Handling if it's $0.00:

public function canDisplay()
{   
    $amount = $this->getAmount();

    /* TO DISPLAY SHIPPING & HANDLING IN INVOICE PDF IF ITS $0.00 - PARTAB SAIFUDDIN */
    if(strtolower($this->getTitle()) == "shipping & handling" && $amount == 0){
        return true;
    }

    return $this->getDisplayZero() === 'true' || $amount != 0;
}

and I got the result as I want, but when I override this file it didn't work and I find out that "Shipping & Handling" doesn't exist in echo $this->getTitle(), these are the titles which I got in a file which I override:

Discount
Adjustment Refund
Adjustment Fee

and these are the titles which I got in core file:

Subtotal
Tax
Shipping & Handling
Grand Total

I don't understand why I'm not getting "Shipping & Handling" in my overridden file...

Thank You.

2 Answers 2

3

I would suggest, using the XML approach here instead of an override, as overrides can easily break things and create incompatibilities. In vendor/magento/module-sales/etc/pdf.xml we find the following code block:

        <total name="shipping">
            <title translate="true">Shipping &amp; Handling</title>
            <source_field>shipping_amount</source_field>
            <font_size>7</font_size>
            <display_zero>false</display_zero>
            <sort_order>400</sort_order>
        </total>

The interesting part here is the <display_zero> node, which is also used by default in the canDisplay() function. So to solve that problem, I created a custom module which requires Magento_Sales and add an own pdf.xml with the following content:

<?xml version="1.0"?>
<config>
    <totals>
        <total name="shipping">
            <display_zero>true</display_zero>
        </total>
    </totals>
</config>

This displays the shipping amount regardless of the actual value.

1
0

So, the issue is resolved, there is a file /vendor/magento/module-tax/Model/Sales/Pdf/Shipping.php, I override this file and add canDisplay() function in it:

public function canDisplay()
{
    $amount = $this->getAmount();
    /* TO DISPLAY SHIPPING & HANDLING IN INVOICE PDF IF ITS $0.00 - PARTAB SAIFUDDIN */
    if(strtolower($this->getTitle()) == "shipping & handling" && $amount == 0){
        return true;
    }
    return $this->getDisplayZero() === 'true' || $amount != 0;
}

and I get result as I want:

enter image description here

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.