1

I set my M2.3 to Deutsch but the grand total is still with $ symbol, please can you help with this issue?

Checkout grand total

1 Answer 1

2

This is a "feature" of Magento. If you perform the checkout in a different currency as the base currency, you see an extra line in the totals with the total converted to the base currency. You can remove this by creating a mixin on the 'Magento_Tax/js/view/checkout/summary/grand-total js compoenent. and a plugin on the Magento\Sales\Block\Order\Totals class.

So create a new module. Let's call it Easylife_OrderTotal with the following files:

registration.php - the module registration file

<?php

declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Easylife_OrderTotal', __DIR__);

composer.json - composer file

{
    "name": "easylife/module-order-total",
    "description": "Module that removes the base currency total from checkout and sales",
    "type": "magento2-module",
    "license": "MIT",
    "minimum-stability": "dev",
    "require": {
        "magento/framework": "*",
        "magento/module-tax": "*",
        "magento/module-sales": "*",
        "magento/module-checkout": "*",
        "php": "~7.1.3||~7.2.0||~7.3.0||~7.4.0"
    },
    "autoload": {
        "psr-4": {
            "Easylife\\OrderTotal\\": ""
        },
        "files": [
            "registration.php"
        ]
    }
}

etc/module.xml - the module declaration file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Easylife_OrderTotal">
        <sequence>
            <module name="Magento_Checkout"/>
            <module name="Magento_Sales"/>
            <module name="Magento_Tax"/>
        </sequence>
    </module>
</config>

etc/di.xml - declare a plugin for the totals block

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Block\Order\Totals">
        <plugin name="remove-base-grand-total" type="Easylife\OrderTotal\Plugin\Sales\Block\Order\TotalsPlugin" />
    </type>
</config>

Plugin/Sales/Block/Order/TotalsPlugin.php - the plugin that removes the base currency total.

<?php

declare(strict_types=1);

namespace Easylife\OrderTotal\Plugin\Sales\Block\Order;

use Magento\Framework\DataObject;
use Magento\Sales\Block\Order\Totals;

class TotalsPlugin
{
    /**
     * Remove the base grand total
     *
     * @param Totals $subject
     * @param array $totals
     * @return array
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGetTotals(Totals $subject, $totals)
    {
        if (is_array($totals)) {
            return array_filter(
                $totals,
                function (DataObject $total) {
                    return $total->getData('code') !== 'base_grandtotal';
                }
            );
        }
        return $totals;
    }
}

view/frontend/requirejs-config.js - declare the plugin on the checkout grand total component

var config = {
    config: {
        mixins: {
            'Magento_Tax/js/view/checkout/summary/grand-total': {
                'Easylife_OrderTotal/js/mixin/checkout/summary/grand-total': true
            }
        }
    }
};

view/frontend/web/js/mixin/checkout/summary/grand-total.js - the mixin on the grand total component

define(function () {
    'use strict';

    var mixin = {
        isBaseGrandTotalDisplayNeeded: function () {
            return false;
        }
    };
    return function (target) {
        return target.extend(mixin);
    };
});

Bonus: Test/Unit/Plugin/Sales/Block/Order/TotalsPluginTest.php - unit test for your plugin

<?php

declare(strict_types=1);

namespace Easylife\OrderTotal\Test\Unit\Plugin\Sales\Block\Order;

use Magento\Framework\DataObject;
use Magento\Sales\Block\Order\Totals;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Easylife\OrderTotal\Plugin\Sales\Block\Order\TotalsPlugin;

class TotalsPluginTest extends TestCase
{
    /**
     * @covers \Easylife\OrderTotal\Plugin\Sales\Block\Order\TotalsPlugin::afterGetTotals
     */
    public function testAfterGetTotals()
    {
        /** @var Totals | MockObject $subject */
        $subject = $this->createMock(Totals::class);
        $plugin = new TotalsPlugin();
        $total1 = new DataObject(['code' => 'total1']);
        $total2 = new DataObject(['code' => 'total2']);
        $baseGrandTotal = new DataObject(['code' => 'base_grandtotal']);
        $total3 = new DataObject(['code' => 'total3']);
        $totals = [0 => $total1, 1 => $total2, 2 => $baseGrandTotal, 3 => $total3];
        $expected = [0 => $total1, 1 => $total2, 3 => $total3];
        $this->assertEquals($expected, $plugin->afterGetTotals($subject, $totals));
        //test for not array totals
        $this->assertEquals('dummy', $plugin->afterGetTotals($subject, 'dummy'));
    }
}

run setup:upgrade to install the module, setup:static:content-deploy -f to deploy the static resources and check your checkout and order details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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