Magento – how to get refreshed shipping address in quote object in js – Magento2.2

cachemagento2magento2.2quote

I am using this one code but once i update shipping info not getting refreshed info they retrieve old info how can i achieve that?

require([
    'jquery',
    'Magento_Checkout/js/model/quote',
    'Magento_Checkout/js/model/shipping-service',
    'Magento_Checkout/js/model/shipping-rate-registry',
    'Magento_Checkout/js/model/shipping-rate-processor/customer-address',
    'Magento_Checkout/js/model/shipping-rate-processor/new-address',
], function($, quote, shippingService, rateRegistry, customerAddressProcessor, newAddressProcessor) {
    $('#target').on('click', function(e) {
        var address = quote.shippingAddress();
        console.log(address);
        // clearing cached rates to retrieve new ones
        rateRegistry.set(address.getCacheKey(), null);

        console.log(quote.shippingAddress()); // but getting old one 
        var type = quote.shippingAddress().getType();
        if (type) {
            customerAddressProcessor.getRates(address);
        } else {
            newAddressProcessor.getRates(address);
        }
    });
});

Best Answer

Try this:

require([
        'jquery',
        'Magento_Checkout/js/model/quote',
        'Magento_Checkout/js/model/shipping-service',
        'Magento_Checkout/js/model/shipping-rate-registry',
        'Magento_Checkout/js/model/shipping-rate-processor/customer-address',
        'Magento_Checkout/js/model/shipping-rate-processor/new-address',
    ], function($, quote, shippingService, rateRegistry, customerAddressProcessor, newAddressProcessor) {
        $('#target').on('click', function(e) {
            var address = quote.shippingAddress();
            console.log(address);

            // reload address information
            address.trigger_reload = new Date().getTime();

            // clearing cached rates to retrieve new ones
            rateRegistry.set(address.getCacheKey(), null);

            console.log(quote.shippingAddress()); // but getting old one 
            var type = quote.shippingAddress().getType();
            if (type) {
                customerAddressProcessor.getRates(address);
            } else {
                newAddressProcessor.getRates(address);
            }
        });
    });