Magento – Magento 2 Show custom fee in admin invoice pdf

admininvoicemagento-2.1pdf

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…

Best Answer

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;
    }
}
Related Topic