0

As I mentioned in the headline I need to alter the Grand Total to be Charged section in the order email. My store is running in LKR as Base currency and AUD as another currency.

If I place an order in AUD, I am getting the order email like below. Which is showing the Grand Total to be Charged in LKR.

enter image description here

I used an override the Magento\Sales\Block\Order\Totals.php like below.

My di.xml file is like this.

<?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="Ayakil\AudDevelopment\Rewrite\Magento\Sales\Block\Order\Totals"/>
</config>

My Totals.php in the ( Ayakil\AudDevelopment\Rewrite\Magento\Sales\Block\Order ) is like below.

protected function _initTotals()
{
if ($this->getOrder()->isCurrencyDifferent()) {
        $this->_totals['base_grandtotal'] = new \Magento\Framework\DataObject(
            [
                'code' => 'base_grandtotal',
                'value' => $source->getGrandTotal(),
                'label' => __('My custom text'), // changed
                'is_formated' => true,
            ]
        );
    }
}

And tried to remove the base_grandtotal too like this. I am getting the email like below after this modification.

enter image description here

$this->removeTotal('base_grandtotal');

3 Answers 3

1

The _initTotals is protected method, you can't use plugin for that.

So there are 2 ways to do this:

  1. Overwrite the class, so you can do whatever you want.
  2. The better way is to add your own initTotal.

Here I explain the second way, as you can see in the Block, initTotals is invoked from beforeHtml

enter image description here

According to the logic, if a child has its own initTotals function, it will be invoked.

So you can write your own class, which will have a initTotals method and then do something like $this->_totals['base_grandtotal'] = your custom

You can search for magento code to see example about it, for ex this class has initTotals function of its own \Magento\Sales\Block\Adminhtml\Order\Creditmemo\Totals

5
  • thank you for your answer, In my code I am trying to overwrite it right?
    – Mujahidh
    Mar 11, 2021 at 4:55
  • Your code uses plugin which is not correct, because plugin cannot work with protected methods.
    – Jimmy
    Mar 11, 2021 at 9:43
  • I am not using a plugin here, I just override the totals.php, please explain
    – Mujahidh
    Mar 11, 2021 at 9:46
  • My bad, just check your code, it's overwritten. Did you post the full code or just a part of it? If it's a full code then you should rewrite your code. You need to have pretty much the same code as the old class and then make changes in your code. (copy the whole initTotals function and then change what needed in your code)
    – Jimmy
    Mar 11, 2021 at 10:11
  • 1
    thank you, I copied entire inittotals method to my overtite totals.php and changed it, it worked for me
    – Mujahidh
    Mar 11, 2021 at 11:19
1

You could also remove the "Grand Total To Be Charged" line by changing vendor\magento\module-sales\view\frontend\templates\order\totals.phtml. You can achieve this by:

  1. First, create a simple Magento 2 theme.
  2. Then, copy vendor\magento\module-sales\view\frontend\templates\order\totals.phtml to <your_theme_directory>\Magento_Sales\templates\order\totals.phtml
  3. At the opening php tag (line 10) add the following like of code $block->removeTotal('base_grandtotal');.

You're totals.phtml file should then look something like this:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * @var $block \Magento\Sales\Block\Order\Totals
 * @see \Magento\Sales\Block\Order\Totals
 */

/* Remove 'Grand Total to be Charged' block from totals */
$block->removeTotal('base_grandtotal');

?>
<?php foreach ($block->getTotals() as $_code => $_total) : ?>
    <?php if ($_total->getBlockName()) : ?>
        <?= $block->getChildHtml($_total->getBlockName(), false) ?>
    <?php else :?>
...

Hope this helps!

0

I used to copy the entire _initTotals() method from the original file and paste it to overwrite the file and did the changes to base_grandtotal like below.

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

This worked for me, I added

'value' => $this->getOrder()->formatPrice($source->getGrandTotal()),

instead of

'value' => $this->getOrder()->formatBasePrice($source->getBaseGrandTotal()),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.