How To

How to Update Shipping Methods when Street Field Input Change on Checkout Page in Magento 2

Hello Magento Friends,

Today I will explain How to Update Shipping Methods when Street Field Input Change on Checkout Page in Magento 2.

Magento 2 supports different shipping methods like FedEx, DHL, UPS, USPS, and more. You can also Create Custom Shipping Method in Magento 2.

On the Magento 2 checkout page, the user enters the address and selects their desired shipping method. But the same shipping methods are not available for all the regions. When the user makes input changes in the street field, the shipping methods provided must change based on that particular region.

To achieve this, follow the below steps.

Steps to Update Shipping Methods when Street Field Input Change on Checkout Page in Magento 2:

Step 1: Add a mixin for shipping rates validation rules. For that, create a file at the below path

app\code\Vendor\Extension\view\frontend\requirejs-config.js

Now, add the below code

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

Step 2: Now go to the following path

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

Add the below code. 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 front end.

Conclusion:

This way you can Update Shipping Methods when Street Field Input Change on Checkout Page in Magento 2. Also, check out Shipping and Payment Method per Customer Group Extension which allows the store owner to manage shipping and payment method for the specific customer groups. If you have any doubts, just mention them in the comment box. Share the article with your friends and social media handles. 

Happy Coding!

Click to rate this post!
[Total: 6 Average: 4.2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

6 hours ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

7 hours ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

2 days ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

5 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 week ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 week ago