Skip to main content

Magento 2 Adding Total Due and Total Paid to PDF order

Hi I am trying to add total paid and total due lines on my Magento PDF Invoice.

Firstly you have to create pdf.xml at path :

app\code\Pawan\PdfCustomiser\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="total_due">
            <title translate="true">Total Due</title>
            <source_field>total_due</source_field>
            <model>Pawan\PdfCustomiser\Model\Sales\Pdf\Totaldue</model>
            <font_size>7</font_size>
            <display_zero>false</display_zero>
            <sort_order>750</sort_order>
         </total>
         <total name="total_paid">
            <title translate="true">Total Paid</title>
            <source_field>total_paid</source_field>
            <model>Pawan\PdfCustomiser\Model\Sales\Pdf\Totalpaid</model>
            <font_size>7</font_size>
            <display_zero>false</display_zero>
            <sort_order>700</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\Pawan\PdfCustomiser\Model\Sales\Pdf\Totalpaid.php

<?php

namespace Pawan\PdfCustomiser\Model\Sales\Pdf;

class Totalpaid extends \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal
{
    /**
     * @var \Magento\Tax\Model\Config
     */
    protected $_taxConfig;

    /**
     * @param \Magento\Tax\Helper\Data $taxHelper
     * @param \Magento\Tax\Model\Calculation $taxCalculation
     * @param \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory $ordersFactory
     * @param \Magento\Tax\Model\Config $taxConfig
     * @param array $data
     */
    public function __construct(
        \Magento\Tax\Helper\Data $taxHelper,
        \Magento\Tax\Model\Calculation $taxCalculation,
        \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory $ordersFactory,
        \Magento\Tax\Model\Config $taxConfig,
        array $data = []
    ) {
        $this->_taxConfig = $taxConfig;
        parent::__construct($taxHelper, $taxCalculation, $ordersFactory, $data);
    }

    /**
     * Check if tax amount should be included to grandtotals block
     * array(
     *  $index => array(
     *      'amount'   => $amount,
     *      'label'    => $label,
     *      'font_size'=> $font_size
     *  )
     * )
     * @return array
     */
    public function getTotalsForDisplay()
    {
//        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/pdftotalstotaldue.log');
//        $logger = new \Zend\Log\Logger();
//        $logger->addWriter($writer);
//        $logger->info("sdjkhsahda". $this->getSource()->getTotalPaid());
        $store = $this->getOrder()->getStore();
        
        if (!$this->_taxConfig->displaySalesTaxWithGrandTotal($store)) {
            return parent::getTotalsForDisplay();
        }
        $totalpaid = $this->getOrder()->formatPriceTxt($this->getSource()->getTotalPaid());
        
        $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;

        $totals = [
            [
                'amount' => $this->getAmountPrefix() . $totalpaid,
                'label' => __('Total Paid') . ':',
                'font_size' => $fontSize,
            ],
        ];

        $totals[] = [
            'amount' => $this->getAmountPrefix() . $totalpaid,
            'label' => __('Total Paid') . ':',
            'font_size' => $fontSize,
        ];
        return $totals;
    }
}

same way new file Totaldue.php and small changes in place of $this->getSource()->getTotalPaid() replace with $this->getSource()->getTotalDue()

Next clear flush cache
after this your fields will be added in downloaded pdf of an order.

Comments

  1. Thanks for sharing informative knowledge about total due total paid PDF. Some other platform switch e-commerce site to Magento because of easily available Magento code.

    ReplyDelete
  2. Thanks for sharing this news here. This blog and solution you provided in this blog is really helpful.

    ReplyDelete

Post a Comment

Popular posts from this blog

Magento 2 product collection Filtering multi-select attribute values

  If you have multi-select attribute of product like below If you want filter value for this option Use below syntax to get product data: ->addAttributeToFilter('store_model', array('finset' => $params['store_model'])) finset key is used for multiselect attribute filter. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $products = $objectManager->get('Magento\Catalog\Model\Product')         ->getCollection()         ->addAttributeToSelect('*')         ->addAttributeToSelect('store_brand')         ->addAttributeToSelect('store_model')         ->addAttributeToSelect('store_year')         ->addAttributeToFilter('store_brand', array('finset' => $params['store_brand']))         ->addAttributeToFilter('store_model', array('finset' => $params['store_model']))         ->ad...

magento 2 best seller product display based on current category and subcategories

Magento 2 getting best seller based on category wise displaying i have done below for that to implement this functionality it is working fine for me. step 1 create block file in our module folder <?php namespace Pawan\Bestseller\Block; use Magento\Catalog\Api\CategoryRepositoryInterface; class Bestsellercategory extends \Magento\Catalog\Block\Product\ListProduct {     /**      * Product collection model      *      * @var Magento\Catalog\Model\Resource\Product\Collection      */     protected $_collection;     /**      * Product collection model      *      * @var Magento\Catalog\Model\Resource\Product\Collection      */     protected $_productCollection;     /**      * Image helper      *      * @var Magento\Catalog\Helper\Image     ...

magento 1 extension converted in to magento 2 process

Simple Install =================================== Download as a zip below link. unzip and upload it to your public_html or other rootdoc directory. https://github.com/magento/code-migration. login to your server via shell switch to the code-migration-develop directory by running following command   cd code-migration-develop Run command following command in root directory of this toolkit composer instal Before running the migration, the following directories need to be prepared: ===================================== * `<src>` - Directory that contains custom Magento 1.x code that is intended to be migrated. The code must follow the Magento 1.x directory structure.   Magento1.x core files must not be included. * `<dst>` - Empty directory for the toolkit to put the generated Magento 2 code to * `<m1>` - Directory that contains: * Vanilla Magento 1.x codebase, and * Custom Magento 1.x code same as...