Display Extra Fee to The Total of Order Invoice PDF In Magento 2
A detailed invoice is essential to enhance your customers’ shopping experience for them to understand the pricing breakdown of the product completely.
For instance, if there are any extra charges on delivery or the shoppers have opted for an EMI option. All information must display in your invoice with a proper breakdown.
If the shopper opts for an EMI option, they should know the amount they have paid with the balance they must pay in the future.
It might be a limiting factor for you as a Magento 2 store owner as the default Magento 2 doesn’t offer any built-in feature to show extra fees in order invoices.
In this blog, overcome this limitation and learn how to seamlessly Display Extra Fee to The Total Order Invoice PDF in Magento 2.
Display Extra Fee to The Total of Order Invoice Pdf In Magento 2
Here let us take an example of EMI payments. Here if your shopper has made a payment, so here is a breakdown of how you can showcase the entire detail.
Step 1: Create your pdf.xml file in the Vendor/Module/etc folder.
This code calculates the amount paying now and the remaining amount using the model and displays even if the value is zero.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!--?xml version="1.0"?--> partial_pay_now 7 true Vendor\Module\Model\Sales\Pdf\PayNow 350 partial_pay_later 7 true Vendor\Module\Model\Sales\Pdf\PayLater 360 |
Step 2: Now create Paynow.php file in Vendor\Module\Model\Sales\Pdf folder
This code calculates the amount your shopper paid using the partial payment model. The information used here will shown under “Amount Paying Now” in the PDF.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace Vendor\Module\Model\Sales\Pdf; use Magento\Sales\Model\Order\Pdf\Total\DefaultTotal; class PayNow extends DefaultTotal { /** * Get array of arrays with totals information for display in PDF * array( * $index => array( * 'amount' => $amount, * 'label' => $label, * 'font_size'=> $font_size * ) * ) * @return array */ public function getTotalsForDisplay(): array { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $partialpayment = $objectManager->create('\Vendor\Module\Model\Partialpayment'); $partialPayment = $partialpayment->load($this->getOrder()->getIncrementId(),'order_id'); $paynow = $partialPayment->getPaidAmount(); if (!$partialPayment->getOrderId()) { return []; } $amountInclTax = $this->getOrder()->formatPriceTxt($paynow); $fontSize = $this->getFontSize() ? $this->getFontSize() : 7; return [ [ 'amount' => $this->getAmountPrefix() . $amountInclTax, 'label' => __($this->getTitle()) . ':', 'font_size' => $fontSize, ] ]; } } |
Step 3: Create PayLater.php at Vendor\Module\Model\Sales\Pdf Folder
This code calculates the remaining amount your shopper needs to pay. The PHP uses the custom partial payment model. The information used here will show under “Remaining Amount” in the PDF.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace Vendor\Module\Model\Sales\Pdf; use Magento\Sales\Model\Order\Pdf\Total\DefaultTotal; class PayLater extends DefaultTotal { /** * Get array of arrays with totals information for display in PDF * array( * $index => array( * 'amount' => $amount, * 'label' => $label, * 'font_size'=> $font_size * ) * ) * @return array */ public function getTotalsForDisplay(): array { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $partialpayment = $objectManager->create('\Vendor\Module\Model\Partialpayment'); $partialPayment = $partialpayment->load($this->getOrder()->getIncrementId(),'order_id'); $remainAmount = $partialPayment->getRemainingAmount(); if (!$partialPayment->getOrderId()) { return []; } $amountInclTax = $this->getOrder()->formatPriceTxt($remainAmount); $fontSize = $this->getFontSize() ? $this->getFontSize() : 7; return [ [ 'amount' => $this->getAmountPrefix() . $amountInclTax, 'label' => __($this->getTitle()) . ':', 'font_size' => $fontSize, ] ]; } } |
And it’s completed ! 🎉
Conclusion
Here you make edits to the row based on your requirement. I have considered here the EMI option.
You can make edits here based on any shipping charges or discount codes.
I hope now you can easily display extra fee to the total order invoice PDF in Magento 2. Feel free to ask in the comment section if you have any doubts about this process. I would be happy to help you.
✨Helpful read✨
Prev
10 Best Shopify Themes for Single Product Stores
10 Best Shopify Themes For Mobile [Mobile-First Themes]
Next