To display custom applied fee on Invoice Page you need to create there layout files
- sales_order_invoice_new.xml
- sales_order_invoice_updateqty.xml
- 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;
}
}