Updating order totals in frontend checkout/admin order view - M2

Updating order totals in frontend checkout/admin order view - M2

October 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Updating order totals in frontend checkout/admin order view - M2

Updating Order Totals in Frontend Checkout & Admin Order View - Magento 2

In this guide, we’ll walk you through updating order totals during checkout and on the admin order view page in Magento 2. We’ll address two key requirements: adjusting the totals post-VAT validation and ensuring these changes persist in both frontend and backend.

The Current Challenge

You’re facing two main issues:

  1. JavaScript Changes Not Persisting: Your JavaScript function successfully updates the totals on the checkout page but doesn’t save these changes to the order view in the admin panel.

  2. Model Collect Function Limitations: While your sales.xml configuration allows for total adjustments, you can’t limit these changes to only after VAT validation in the controller.

Our Solution Approach

To resolve this, we’ll implement a two-pronged solution: update totals via JavaScript and ensure persistence through model observers and custom events. Let’s dive into each step:

Step 1: Update Totals via JavaScript (and Ensure Persistence)

Your current JavaScript function is on the right track but lacks the necessary storage to maintain changes in the backend. Here’s how we can modify your code:

define([
    'jquery',
    //... other dependencies
], function ($, quote, urlManager, storage, getPaymentInformationAction, totals, fullscreenLoader) {
    'use strict';

    return function (removeTax, tax) {
        var payload = {
            addressInformation: {...},
            custom_field: removeTax ? -tax : tax  // Add this line
        };

        fullscreenLoader.startLoader();

        return storage.post(
            urlManager.getUrlForSetShippingInformation(quote),
            JSON.stringify(payload)
        ).done(function (response) {
            //... existing code

            var deferred = $.Deferred();
            totals.isLoading(true);

            getTotalsAction([], deferred);

            $.when(deferred).done(function () {
                quote.setTotals(response.totals);
                totals.isLoading(false);
                fullscreenLoader.stopLoader();
            });
        }).fail(function (response) {
            console.log('fail');
            totals.isLoading(false);
            fullScreenLoader.stopLoader();
        });

    };
});

We’ve added a new field custom_field to the payload that stores whether we’re removing or adding tax. This value will be sent alongside your shipping information.

Next, in your controller, you can access this value:

$removeTax = (bool) $request->getParam('custom_field');

if ($removeTax) {
    // Update order totals logic here
}

Step 2: Persist Changes with Model Observers and Custom Events

To ensure changes made in the controller are reflected both on the frontend and in the admin panel, we’ll use a combination of model observers and custom events.

Create a Custom Event for VAT Validation Success

In your controller, after successful VAT validation:

$eventManager = $objectManager->get('\Magento\Framework\Event\ManagerInterface');
$eventManager->dispatch('vat_validation_success', ['validated_vat' => $validVat]);

Create an Observer to Update Totals on Event Trigger

Create a custom observer that listens for the vat_validation_success event and updates the totals accordingly. In your etc/events.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="vat_validation_success">
       <observer status="disabled" name="update_order_totals_after_vat_validation"/>
   </event>
</config>

In your observer:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $validVat = $observer->getEvent()->getValidatedVat();
    // Update totals here using the validVat value
}

Ensure Backend Totals Reflection

Your custom totals model should properly collect and set the totals, reflecting changes made in both frontend JavaScript and backend controller logic.

Conclusion: A Comprehensive Solution

By following this approach—updating totals via enhanced JavaScript functionality, persisting changes with model observers and custom events—you can successfully adjust order totals post-VAT validation and ensure these changes are reflected in both the frontend checkout and admin order view in Magento 2. For more detailed guidance on Magento development, visit Laravel Company.

Magento 2 Checkout Database Extensions

Our network