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.

| improve this question | | | | |
4

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

    }
);
| improve this answer | | | | |
  • 3
    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());            
            /* ... */                
        });
    }
);
| improve this answer | | | | |
0

To reload checkout page shipping methods block

As per your requirement add these dependencies 'Magento_Checkout/js/model/quote','Magento_Checkout/js/model/shipping-rate-registry', on define[]

var address = quote.shippingAddress(); address.trigger_reload = new Date().getTime(); rateRegistry.set(address.getKey(), null); rateRegistry.set(address.getCacheKey(), null);

| improve this answer | | | | |

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.