Question

I have a checkout field for tax exemption that is not related to a customer group. That would be too easy. Instead, I would like to update the tax amount and total to reflect those changes when this field is changed.

I have tried piecing together solutions to similar problems but nothing seems to do the trick.

So far what I have working is an observer on the sales_quote_address_collect_totals_after event that gets the value of my field from the address and updates the tax amount totals. This successfully updates the tax, but does not update the grand total. As far as I can tell I cannot update the grand total in the same way.

I have attempted to call $quote->collectTotals() and save the quote, but without success. It does not update the grand total to the correct amount.

I have attempted to trigger reloading the totals block via javascript, but this was also unsuccessful. If you believe either of these methods should be working I am happy to update with the code I used.

Simply put, I need to update the tax amount, and have that change reflected in the cart total.

Any help would be greatly appreciated!

Was it helpful?

Solution

I was able to get this working using only javascript. A similar solution is below:


define([
    'Magento_Checkout/js/model/quote',
    'Magento_Checkout/js/model/resource-url-manager',
    'Magento_Checkout/js/model/payment-service',
    'mage/storage',
    'Magento_Checkout/js/model/payment/method-converter',
    'Magento_Checkout/js/model/full-screen-loader',
    'Magento_Checkout/js/model/error-processor'
], function(
        quote,
        resourceUrlManager,
        paymentService,
        storage,
        methodConverter,
        fullScreenLoader,
        errorProcessor
) {
    'use strict';

    return function(taxExemptValue) {
        var payload = {
            addressInformation: {
                shipping_address: quote.shippingAddress(),
                billing_address: quote.billingAddress(),
                shipping_method_code: quote.shippingMethod().method_code,
                shipping_carrier_code: quote.shippingMethod().carrier_code
            }
        };

        fullscreenLoader.startLoader();

        storage.post(
            resourceUrlManager.getUrlForSetShippingInformation(quote),
            JSON.stringify(payload)
        ).done(
            function (response) {
                if (taxExemptValue == 1) {
                    var currentTax = response.totals.tax_amount;
                    response.totals.shipping_tax_amount = 0;
                    response.totals.tax_amount = 0;
                    response.totals.base_tax_amount = 0;
                    response.totals.base_grand_total -= currentTax;
                    response.totals.grand_total -= currentTax;
                }

                quote.setTotals(response.totals);
                paymentService.setPaymentMethods(methodConverter(response.payment_methods));
                fullscreenLoader.stopLoader();
            }
        ).fail(
            function (response) {
                errorProcessor.process(response);
                fullscreenLoader.stopLoader();
            }
        );
    };

This would be called when the tax exempt field is changed, passing in it's value.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top