1

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.

2

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());
       }

    }
);
  • 1
    Where should I add this code? – Vivek Kumar Oct 9 '18 at 21:00
0

To trigger shipping methods block update you can run quote.shippingAddress(quote.shippingAddress());

define(
    [
        /* ... */
        'Magento_Checkout/js/model/quote',
        /* ... */        ],
    function (
        /* ... */
        quote,
        /* ... */
    ) {
        'use strict';

        return Component.extend({
            /* ... */
            quote.shippingAddress(quote.shippingAddress());            
            /* ... */                
        });
    }
);

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.