1

I have my own custom discount, and on checkout I can use it. When I place the order, I see that grand total is changed, but me need to show discount. I want to paste it between subtotal and shipping.enter image description here

2 Answers2

2

You can override this file in your custom module

vendor/magento/module-sales/Block/Order/Totals.php

and you can override this function _initTotals() only there, you need to create below files.

Create di.xml file here

app/code/Vendor/Module/etc/di.xml

Content for this file is..

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Block\Order\Totals" type="Vendor\Module\Block\Order\Totals" />
</config>

Create Block file here

app/code/Vendor/Module/Block/Order/Totals.php

Content for this file is..

<?php
namespace Vendor\Module\Block\Order;

use Magento\Sales\Model\Order;

class Totals extends \Magento\Sales\Block\Order\Totals
{
    protected function _initTotals()
    {
        parent::_initTotals();
        $source = $this->getSource();

        $this->_totals = [];
        $this->_totals['subtotal'] = new \Magento\Framework\DataObject(
            ['code' => 'subtotal', 'value' => $source->getSubtotal(), 'label' => __('Subtotal')]
        );


        /*Here you can add your custom code */  
        $this->_totals['custom'] = new \Magento\Framework\DataObject(
            ['code' => 'custom', 'value' => "Custom", 'label' => __('Custom')]
        );
        /*Here you can add your custom code */  

        /**
         * Add shipping
         */
        if (!$source->getIsVirtual() && ((double)$source->getShippingAmount() || $source->getShippingDescription())) {
            $this->_totals['shipping'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'shipping',
                    'field' => 'shipping_amount',
                    'value' => $this->getSource()->getShippingAmount(),
                    'label' => __('Shipping & Handling'),
                ]
            );
        }

        /**
         * Add discount
         */
        if ((double)$this->getSource()->getDiscountAmount() != 0) {
            if ($this->getSource()->getDiscountDescription()) {
                $discountLabel = __('Discount (%1)', $source->getDiscountDescription());
            } else {
                $discountLabel = __('Discount');
            }
            $this->_totals['discount'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'discount',
                    'field' => 'discount_amount',
                    'value' => $source->getDiscountAmount(),
                    'label' => $discountLabel,
                ]
            );
        }

        $this->_totals['grand_total'] = new \Magento\Framework\DataObject(
            [
                'code' => 'grand_total',
                'field' => 'grand_total',
                'strong' => true,
                'value' => $source->getGrandTotal(),
                'label' => __('Grand Total'),
            ]
        );

        /**
         * Base grandtotal
         */
        if ($this->getOrder()->isCurrencyDifferent()) {
            $this->_totals['base_grandtotal'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'base_grandtotal',
                    'value' => $this->getOrder()->formatBasePrice($source->getBaseGrandTotal()),
                    'label' => __('Grand Total to be Charged'),
                    'is_formated' => true,
                ]
            );
        }
        return $this;
    }
}

Hope this will help you!

Kishan Savaliya
  • 7,736
  • 1
  • 12
  • 27
0

With custom discount you mean, you didnt add a new discount via salesrules or something like this, but added a new total fee, which needs to be applied to the quote totals, to be displayed in the checkout ?

To achieve this you need to create a new total model, and attach it to the totals inside a modules etc/sales.xml.

There are several nice guides how to do this, for example this one:
How to add fee to order totals in Magento 2

Khoi Ngo
  • 280
  • 3
  • 7
Ekk4rd
  • 525
  • 2
  • 12
  • I have column in table 'quote', and when we see success page I add this discount to table 'sales_order', me need only to show it. – Віктор Шуст Nov 13 '19 at 15:42
  • Adding a total should always be done by adding a new total model, any other way is far more complicated, or very bad for further extendability. It sounds like you would only need a very simple model to get/set the discount for an quote/order. – Ekk4rd Nov 13 '19 at 15:49