In the world of e-commerce, it is important to provide customers with detailed information about their orders, including any additional fees that may be associated. Unfortunately, the default Magento 2 platform does not have a built-in feature to display extra fees on order invoices. However, with some code enhancements, you can easily incorporate this functionality into your Magento 2 store. In this article, we will provide a step-by-step guide to help you achieve this.


Step 1: Creating the pdf.xml File


The first step is to create a pdf.xml file in your module. This file will define the structure and display properties of the extra fee on the order invoice PDF.


Navigate to your module's etc folder and create a pdf.xml file. In this file, add the following code:

 

<?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="extra_fee">
            <title translate="true">Extra Fee</title>
            <source_field>extra_fee</source_field>
            <font_size>7</font_size>
            <display_zero>true</display_zero>
            <model>Vendor\Module\Model\Sales\Pdf\ExtraFee</model>
            <sort_order>350</sort_order>
        </total>
    </totals>
</config>

In this code snippet, we define the extra_fee total section in the PDF, providing the title, source field (extra_fee), font size, display settings, and the model class to handle the display logic. Adjust the values according to your requirements.


Step 2: Creating the Model Class


Next, we need to create the ExtraFee model class to handle the logic for displaying the extra fee in the PDF.


In your module's Model/Sales/Pdf folder, create a file named ExtraFee.php. In this file, add the following code:

<?php
namespace Vendor\Module\Model\Sales\Pdf;

use Magento\Sales\Model\Order\Pdf\Total\DefaultTotal;

class ExtraFee extends DefaultTotal
{
    /**
     * Get array of arrays with totals information for display in PDF
     * array(
     *      $index => array(
     *          'amount' => $amount,
     *          'label' => $label,
     *          'font_size' => $font_size
     *      )
     * )
     *
     * @return array
     */
    public function getTotalsForDisplay(): array
    {
        $extraFee = $this->getOrder()->getExtraFee();
        
        if ($extraFee === null) {
            return [];
        }
        
        $amountInclTax = $this->getOrder()->formatPriceTxt($extraFee);
        $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;
        
        return [
            [
                'amount' => $amountInclTax,
                'label' => $this->getTitle(),
                'font_size' => $fontSize
            ]
        ];
    }
}

This class extends the DefaultTotal class provided by Magento, and overrides the getTotalsForDisplay() method to fetch the extra fee value from the order and format it for display in the PDF.


Step 3: Modifying the Invoice PDF


To display the extra fee in the order invoice PDF, we need to modify two PHP files: DefaultInvoice.php and Invoice.php in your module.


In DefaultInvoice.php, you will need to modify the array that stores the details of the order, such as product name, SKU, quantity, price, tax, and subtotal. Add the extra fee details to this array, ensuring it is displayed correctly in the invoice PDF. Optionally, you can also include the parent category of the product alongside the product name.


In Invoice.php, you can add an additional table header for the extra fee, allowing it to be displayed in the invoice PDF.


Remember to update your module's module.xml file to indicate any dependencies and di.xml file for new class preferences.


Conclusion


By following these step-by-step instructions, you can easily add an extra fee to the total section of the order invoice PDF in Magento 2. Customizing the invoice PDF to display this extra fee enhances transparency in your e-commerce store and provides customers with a detailed breakdown of their order costs. Implementing this feature showcases the flexibility and customizability of the Magento 2 platform, allowing you to improve the shopping experience for your customers.


Happy Coding!