Magento – Magento 2 checkout: How to trigger shipping methods block update programmatically

checkoutjavascriptmagento-2.1.7magento2

I've seen the What Programmatically Triggers Magento 2's Shipping Rates to Update question, and also read related @AlanStorm's post on his website, but I still don't understand, what I need to add into my custom JS code so that the shipping rates block is updated.

i.e. it automatically updates when postcode field is changed. But even if I manually fire jQuery $(postcode_field).change() trigger, it works only if postcode really changed.

Best Answer

To force shipping method reloading try,

define(
    [
        'Magento_Checkout/js/model/quote',
        'Magento_Checkout/js/model/shipping-rate-processor/new-address',
        'Magento_Checkout/js/model/shipping-rate-processor/customer-address',
        'Magento_Checkout/js/model/shipping-rate-registry'

    ],
    function (quote, defaultProcessor, customerAddressProcessor, rateRegistry) {
       'use strict';

       var processors = [];

       rateRegistry.set(quote.shippingAddress().getCacheKey(), null);

       processors.default =  defaultProcessor;
       processors['customer-address'] = customerAddressProcessor;

       var type = quote.shippingAddress().getType();

       if (processors[type]) {
          processors[type].getRates(quote.shippingAddress());
       } else {
          processors.default.getRates(quote.shippingAddress());
       }

    }
);