Magento 2 – Update Shipping Methods on Street Field Input Change

checkoutfieldinputmagento2

I want to call "estimate-shipping-methods" api on street field input change as it is called when the input of postcode, country field.

Best Answer

Add a mixin for shipping rates validation rules:

app/code/Vendor/Module/view/frontend/requirejs-config.js

var config = {
    config: {
        mixins: {
            'Magento_Checkout/js/model/shipping-rates-validation-rules': {
                'Vendor_Module/js/checkout/model/shipping-rates-validation-rules-mixin': true
            }
        }
    }
};

app/code/Vendor/Module/view/frontend/web/js/checkout/model/shipping-rates-validation-rules-mixin.js

In mixin we will wrap the original rules and add the street:

define([
    'jquery',
    'mage/utils/wrapper'
],function ($, Wrapper) {
    "use strict";

    return function (origRules) {
        origRules.getObservableFields = Wrapper.wrap(
            origRules.getObservableFields,
            function (originalAction) {
                var fields = originalAction();
                fields.push('street');

                return fields;
            }
        );
        
        return origRules;
    };
});

This will add the street rule to each available shipping method on the frontend.

Related Topic