Reading list Switch to dark mode

    Add Custom fee in Downloaded PDF of an order in Magento 2

    Updated 21 February 2024

    To add order’s custom fee amount field in PDF which is downloaded either from admin or from frontend, to can use following process:

    Firstly you have to create pdf.xml at path :

    app\code\Webkul\Test\etc

    <?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="custom_fee">
                <title translate="true">Custom Fee</title>
                <source_field>custom_fee</source_field>
                <model>Webkul\Test\Model\Sales\Pdf\Customfee</model>
                <font_size>7</font_size>
                <display_zero>false</display_zero>
                <sort_order>600</sort_order>
            </total>
        </totals>
    </config>

    Here, source_field: is the field name which you created as a field in order.

    model: is the file in model in which you code to add field in totals.

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    font_size: default font_size is 7 you can change it.

    display_zero: If you don’t want to display field if amount is 0 then set it to false, otherwise set it to true.

    sort_order: sort order for the position of your field.

    Now create file at path:  app\code\Webkul\Test\Model\Sales\Pdf\Customfee.php

    <?php
    
    namespace Webkul\Test\Model\Sales\Pdf;
    
    class Customfee extends \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal
    {
        public function getTotalsForDisplay()
        {
            $amount = $this->getOrder()->formatPriceTxt($this->getAmount());
            if ($this->getAmountPrefix()) {
                $amount = $this->getAmountPrefix() . $amount;
            }
    
            $title = __($this->getTitle());
            if ($this->getTitleSourceField()) {
                $label = $title . ' (' . $this->getTitleDescription() . '):';
            } else {
                $label = $title . ':';
            }
    
            $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;
            $total = ['amount' => $amount, 'label' => $label, 'font_size' => $fontSize];
            return [$total];
        }
    }

    after this your field will be added in downloaded pdf of an order.

    Here are some related links for managing custom fee in order:

    Add Custom Price Fee In Order Total Magento2

    Display Custom Price Fee On Checkout Cart Summary Total Magento2

    Display Custom Price Fee On Sales Order View Page

    I hope this blog will help you with Add Custom fee in Downloaded PDF of an order in Magento 2. You may also check our wide range of best Magento 2 Extensions.

    Please reach out to our team via a support ticket if you have any queries.

    Try this and if you have any queries then just comment below 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • TN CEDRIC
    • Neelesh Singh (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home