Thursday , November 21 2024
Home > Magento Tips & News > Magento 2 Tutorials > Catalog > How to Configure Distance Priority Algorithm in Magento 2 MSI

How to Configure Distance Priority Algorithm in Magento 2 MSI

how-to-configure-distance-priority-algorith- in-magento-2- multi-source-inventory-01

To enhance shipping efficiency and then reduce cost, Magento offers the distance priority algorithm in MSI. This functionality assists in comparing the distance from each source location to the shipping destination and then selecting the nearest source location (physical distance) or the one with the shortest time spent.

There are two ways to calculate the physical distance and time between locations: using database locations imported or Google Maps. This blog will help you precisely understand what they are and configure them in Magento 2 MSI.

Get Started

  • Google MAP uses the latitude and longitude of every single source to measure how close each source location and the shipping location are. To this end, you are required to offer a Google API key with enabled Geocoding API and Distance Matrix API. With this option, you might pay charges through Google.
  • Offline Calculation: By utilizing data downloaded from the website named geonames.org and then imported into Magento stores with a command, this offline option can measure the distance between locations and decide on the closest source. To configure this option in Magento, you need the assistance of a developer in importing geocodes with command-line instructions. 

Explain

  1. The API key is a unique identifier for usage and payment purposes that verifies requests linked with your project. With your project, you must have at least one API key.
  2. Geocoding is the process when addresses (15 Revere Street, Boston, MA, USA, for example) are interpreted into geographic coordinates (Latitude l42.311506, longitude -71.111742). Reverse Geocoding is the opposite.
  3. Geocoding API is a service that provides both geocoding and reverse geocoding of addresses.
  4. Distance Matrix API is a service that calculates the distance and time between a set of source locations (origins) and shipping destinations. The API returns data based on the best path between two locations.

Note: If you use the distance priority algorithm, you had better provide the full street address and GPS coordinates. 

People also read:
The Magento 2 Multi-Source Inventory: An Ultimate Guide
How to Configure Magento 2 Inventory Management

Configure Google Maps

To complete settings and utilize the distance-based algorithm, you need to add a billing account and payment method to your Google account. Despite this, this Google MAP-based option should be chosen rather than Offline calculation due to its advancements and accuracy. 

Step 1: Create the Google API Key

  • If you have logged in to the account, the system will require you to enable billing for the project. Then click Create billing account
enable-billing-for-the-project
  • You are now landing on step 1 of 3, Account Information. Complete the highlighted fields below:
step-1-account-information
  • Step 2 of 3 Identity Verification and Contact Information: Enter your phone number for identity verification and contact information. 
step-2-identity-verification-and-contact-information
  • Step 3 of 3 Payment Information Verification: Complete all billing information. When done, hit on Start My Free Trial.
step-3-payment-information-verification
  • Following this, you need to answer a few questions from Google. Then, hit Submit.
step-4-answer-a-few-questions-1024x625
  • And here is the API key for this project. You will copy this API key to the field Google API Key in the Inventory Configurations
api-key

Optimize Your Magento Store With Powerful Extensions!

Looking for fast, efficient, and well-coded extensions to boost sales for your Magento stores? Then visit the Magezon website and grab the necessary add-ons to optimize yours today!

Step 2: Configure the Google MAP Provider

  • On the admin sidebar, navigate to Store → Settings → Configurations.
admin-sidebar-settings
  • From the left panel, expand to Catalog. Then select Inventory. 
  • The Configuration page appears. Then, scroll down to Distance Provider for Distance-Based SSA and expand this section. After that, unmark the checkbox Use system value so that you can select Google MAP as Provider.
distance-provider-for-distance-based-ssa
How to configure Distance Priority Algorithm in Magento 2 MSI
  • Under the Google Distance Provider section:
google-distance-provider
  • Google API Key: Enter the API key from your Google account. 
  • Computation mode: Untick the checkbox Use system value before selecting an option to get requests for the standard directions for driving (default), walking, and bicycling.
computation-mode
  • Value: Select one of the two following types:
  1. Distance: Choose this option if you want the algorithm to return the distance from the source location to the shipping address in either metrics (kilometers and meters) or imperial (miles and feet). 
  2. Time to Destination: Set Value to this option if you want the algorithm to return the amount of time it takes to go from the source location to the destination in hours and minutes.
value

After all necessary changes are made, hit Save Config.

Configure Offline Calculation

The other way to calculate the distance between the two locations is to use country codes. An Inventory Management CLI command needs to be issued so that you can download and import data from geonames.org. In this case, support from a developer is necessary. 

Explain

  • A geocode is deemed a short, unique identifier of a geographic entity, differentiating from other entities. It is a code that humans can understand. 

For some countries (Canada, Chile, Ireland, Malta, Argentina, and Brazil), there are limitations on geocodes imported from the mentioned website. For further information, refer to the GeoNames Postal Code files.

Step 1: Download and Import Geocodes 

First, download the geocode of the countries in which to ship and have sources from geonames.org. Then, import the following command into your Magento root folder. To do it, enter the space-separated country codes in the command:

bin/magento inventory-geonames:import <country code> <country code> ...

Below is our example:

bin/magento inventory-geonames:import vn us th de

As a result, the system shows the notification after importing the geocode data into your database.

Importing <country code>: OK

Step 2: Configure the Distance Provider as an Offline Calculation

  • Head back to the left panel in the Inventory Configurations
  • Expand the Distance Provider for the Distance-Based SSA section. Choose Online calculation as the distance provider.
offline-caculation
How to configure Distance Priority Algorithm in Magento 2 MSI

Wrap It up

Distance Priority Algorithm in Magento 2 MSI is an efficient supporter in facilitating warehouse and shipping. Using Google MAP or Offline Calculation, you can calculate the distance between the shipping destination and source location. Hopefully, this blog can help you understand deeper the distance priority algorithm in Magento and how to put it into use.

Optimize Your Magento Store With Powerful Extensions!

Looking for fast, efficient, and well-coded extensions to boost sales for your Magento stores? Then visit the Magezon website and grab the necessary add-ons to optimize yours today!

About Nga Dong

Nga Dong
A content writer and marketer in Magezon

Check Also

Magento Grouped Product vs Bundle Product: Key Differences

Being known as Magento product types which are made up of various single products, Magento …

Leave a Reply