4

I have created a module to add custom fee value. I want to show this custom fee in all pages(frontend:order view page, print page etc, backend:order view page, sales invoice pdf, emails)

I have shown it all pages except for admin side invoice pdf. How can I do that? Which XML do I need to edit?

I have tried editing the layout in adminhtml sales_order_invoice_new.xml, sales_order_invoice_updateqty.xml, sales_order_invoice_view.xml

Please help...

2
  • You should accept an answer to help other developers to find their right solution. Commented Mar 27, 2018 at 8:36
  • how to display in order view page? Commented Jan 7, 2020 at 6:50

1 Answer 1

3

To display custom applied fee on Invoice Page you need to create there layout files

  1. sales_order_invoice_new.xml
  2. sales_order_invoice_updateqty.xml
  3. sales_order_invoice_view.xml

Here I made a full module to display custom fee to order, invoice, credit memo with admin configuration you can take a reference or use this module: https://github.com/mageprince/magento2-extrafee/

Here I give one file example:

app/code/Prince/Extrafee/view/adminhtml/layout/sales_order_invoice_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="invoice_totals">
            <block class="Prince\Extrafee\Block\Adminhtml\Sales\Order\Invoice\Totals"
                   name="fee">
            </block>
        </referenceBlock>
    </body>
</page>

app/code/Prince/Extrafee/Block/Adminhtml/Sales/Order/Invoice/Totals.php

<?php

namespace Prince\Extrafee\Block\Adminhtml\Sales\Order\Invoice;

class Totals extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Prince\Extrafee\Helper\Data
     */
    protected $_helper;
    /**
     * @var \Magento\Sales\Model\Order\Invoice
     */
    protected $_invoice = null;
    /**
     * @var \Magento\Framework\DataObject
     */
    protected $_source;
    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Prince\Extrafee\Helper\Data $helper
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Prince\Extrafee\Helper\Data $helper,
        array $data = []
    ) {
        $this->_helper = $helper;
        parent::__construct($context, $data);
    }
    public function getSource()
    {
        return $this->getParentBlock()->getSource();
    }
    public function getInvoice()
    {
        return $this->getParentBlock()->getInvoice();
    }
    public function initTotals()
    {
        $this->getParentBlock();
        $this->getInvoice();
        $this->getSource();
        if(!$this->getSource()->getFee()) {
            return $this;
        }
        $total = new \Magento\Framework\DataObject(
            [
                'code' => 'fee',
                'value' => $this->getSource()->getFee(),
                'label' => $this->_helper->getTitle(),
            ]
        );
        $this->getParentBlock()->addTotalBefore($total, 'grand_total');
        return $this;
    }
}
2
  • Nice one bro!!! Commented Apr 24, 2021 at 15:27
  • Sorry but not showing in invoice PDF. Commented Jun 29, 2023 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.