Reading list Switch to dark mode

    Add processing Fee in Order Email In Magento 2

    Updated 9 January 2023

    Whenever we do customize our store, there the situation comes when we add processing fee to the order.
    and If we have added fee to the order then we must want that fee to be shown in mails as well,

    So today we are going to learn , how to add processing fee to the order emails.
    It is very simple, we just need to follow two steps :
    Step 1 : In your module create a file named as sales_email_order_items.xml , in this location <Vendor>\<Module>\view\frontend\layout\sales_email_order_items.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="order_totals">
                <block class="<Vendor>\<Module>\Block\Total\Processingfee" name="processing-fee"/>
            </referenceBlock>
        </body>
    </page>
    

    Step 2 : This xml will add the block row in order email now lets create a block for the processing-fee ,

    <?php
    namespace <Vendor>\<Module>\Block\Total;
    
    class Processingfee extends \Magento\Framework\View\Element\Template
    {
        /**
         * Tax configuration model
         *
         * @var \Magento\Tax\Model\Config
         */
        protected $config;
    
        /**
         * @var Order
         */
        protected $order;
    
        /**
         * @var \Magento\Framework\DataObject
         */
        protected $source;
    
        /**
         * @param \Magento\Framework\View\Element\Template\Context $context
         * @param \Magento\Tax\Model\Config $taxConfig
         * @param array $data
         * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
         */
        public function __construct(
            \Magento\Framework\View\Element\Template\Context $context,
            \Magento\Tax\Model\Config $taxConfig,
            array $data = []
        ) {
            $this->config = $taxConfig;
            parent::__construct($context, $data);
        }
    
    
        /**
         * Get data (totals) source model
         *
         * @return \Magento\Framework\DataObject
         */
        public function getSource()
        {
            return $this->source;
        }
    
          /**
           * @return Order
           */
        public function getOrder()
        {
            return $this->order;
        }
    
        /**
         * Initialize all order totals relates with tax
         *
         * @return \Magento\Tax\Block\Sales\Order\Tax
         */
        public function initTotals()
        {
    
            $parent = $this->getParentBlock();
            $this->order = $parent->getOrder();
            
            $this->source = $parent->getSource();
            $store = $this->getStore();
            $order = $this->order->load($this->order->getId());
            $processingFee = $order->getData('processing_fee');
            if ($processingFee) {
                $charges = new \Magento\Framework\DataObject(
                    [
                        'code' => 'processing_fee',
                        'strong' => false,
                        'value' => $processingFee,
                        'label' => __('Processing Fee'),
                    ]
                );
                $parent->addTotal($charges, 'processing_fee');
            }
                return $this;
        }
    }
    

    That’s it and we are ready to add the processing fee block in order email.
    if you have any out then please add a comment in comment section below
    Happy Coding .. ­čÖé

    Searching for an experienced
    Magento Company ?
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    4 comments

  • Vishal THakur
    • Prabhat Rawat (Moderator)
  • Olivia Katie
    • Prabhat Rawat (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home