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.
Table of contents
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:
- 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.
- 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.
- Geocoding API is a service that provides both geocoding and reverse geocoding of addresses.
- 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
- Go to Google Maps Platform at https://cloud.google.com/maps-platform/
- Click the Get Started button in the middle or on the right side of the screen.
- If you have logged in to the account, the system will require you to enable billing for the project. Then click Create billing account.
- You are now landing on step 1 of 3, Account Information. Complete the highlighted fields below:
- Step 2 of 3 Identity Verification and Contact Information: Enter your phone number for identity verification and contact information.
- Step 3 of 3 Payment Information Verification: Complete all billing information. When done, hit on Start My Free Trial.
- Following this, you need to answer a few questions from Google. Then, hit Submit.
- 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.
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.
- 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.
- Under the Google Distance Provider section:
- 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.
- Value: Select one of the two following types:
- 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).
- 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.
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.
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!