Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    Add custom fee in downloaded PDF of an order magento2

     

    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.

    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

    Hope it will help you.

    Thank you.

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close