All guides

Distance-based delivery pricing in WooCommerce: a plugin you can build today

· 4 min read

WooCommerce includes flat rate, free shipping, and local pickup out of the box. Extend it with plugins and you can price by weight, item count, or order total. What you cannot do with any of those approaches is charge based on how far the driver has to travel.

For a florist, a bakery, or any business that delivers within a fixed area, distance is what determines cost. A tiered table solves it: deliveries up to five miles cost one amount, up to ten miles cost more, and so on. A small plugin can wire that table to the Google Distance Matrix API and surface it as a proper WooCommerce shipping method that calculates the cost automatically from the customer's address.

What one build gives you

  • A custom WooCommerce shipping method that calculates delivery cost from driving distance
  • Admin settings for your Google Maps API key, store address, and a configurable distance-to-price tier table with five default rows
  • Automatic distance lookup at checkout using the Google Maps Distance Matrix API
  • Unavailability when the customer address falls outside all configured tiers

What it does not do

  • Blocking orders from customers beyond a maximum delivery radius with an explicit message
  • Distance calculation in kilometers instead of miles
  • Traffic-adjusted routing or time-of-day delivery pricing
  • Caching Distance Matrix API responses between checkout sessions to reduce API costs

Flexible Shipping does these. This covers the part most sites use.

The prompt

Loads into the composer so you can edit it first. Nothing is built, and nothing is charged, until you send it.

Build a WordPress plugin for WooCommerce that adds a distance-based local delivery shipping method. Register a custom WooCommerce shipping method with the ID local_delivery_distance and the title Local Delivery. In the method admin settings panel add: a Google Maps API Key text field, a Store Address text field used as the origin for distance calculations, and a custom HTML table for delivery tiers with three columns: minimum miles, maximum miles, and price in dollars. Include an Add Tier button to add rows and a Remove button on each row. Pre-populate the table with five default tiers: 0 to 5 miles at 3.00, 5 to 10 miles at 8.00, 10 to 15 miles at 14.00, 15 to 20 miles at 21.00, and 20 to 30 miles at 30.00. In the calculate_shipping method, build the destination address from the WooCommerce customer shipping fields, send a request to the Google Maps Distance Matrix API with the stored API key and store address as origin, parse the distance value in meters from the response and convert to miles, find the first matching tier where the distance falls between minimum and maximum, and call add_rate with the matched price and the method title. If no tier matches or the API request fails, add no rate so the method is unavailable at checkout.

Build this pluginAbout 55 credits · the free plan includes enough for one

Why the built-in options fall short

WooCommerce's built-in shipping options and most third-party plugins calculate rates from what is in the cart: weight, dimensions, item count, or order value. That works well for carriers that use those same variables to set their own prices.

It does not help a store where the only variable is the delivery address. The official Table Rate Shipping plugin adds tiered rules, but the tiers are based on cart contents, not geography. Flexible Shipping and similar tools follow the same pattern. None of them know how far away the customer is.

What the plugin delivers

A Steem build for this job registers a custom WooCommerce shipping method called Local Delivery. Inside WooCommerce's shipping settings you configure it with a Google Maps API key, your store address, and a table of distance tiers.

Getting a Google Maps API key

Go to the Google Cloud Console, create or select a project, and enable the Distance Matrix API. Under Credentials, create an API key and restrict it to the Distance Matrix API. If your server has a fixed IP address, add an IP restriction as well.

Paste the key into the Local Delivery settings in WooCommerce. It is stored in the shipping method options alongside your store address and tier table, with no code changes needed.

When a paid plugin makes more sense

If you want to block orders from addresses beyond a certain radius rather than just return a price for them, this build is not the right tool. It will show as unavailable for addresses outside your tier range, but it does not enforce a hard boundary with an explicit user-facing message.

If you need kilometer support, real-time traffic-based routing, or integration with a third-party logistics service, look at the paid options in that space. This build does one thing: calculate the delivery cost based on how far the customer is from your store.

Questions

Does the Google Distance Matrix API cost money?
Yes, though for a small local delivery store the cost is very low. Google includes a monthly credit across Maps Platform products, and a store handling a typical local delivery volume is unlikely to exceed it. Check Google Cloud pricing for current rates.
Does this work with WooCommerce shipping zones?
Yes. You add the Local Delivery method to a shipping zone the same way you would add any other method. Use a zone to limit which regions see the method at all, then let the distance tiers handle pricing within it.
What does the customer see if they are outside my delivery tiers?
The Local Delivery method shows as unavailable at checkout. They can still complete the order using any other shipping method you have configured. If you want a message explaining why delivery is unavailable, that is a second Steem build on top of this one.
Can I add or edit tiers after installing the plugin?
Yes. The tier table in the WooCommerce shipping method settings is fully editable. Add rows, remove rows, or change any distance or price value at any time. Changes take effect on the next checkout.
Can I set this up for multiple store locations?
This build uses one store address as the origin. For multiple locations, paste the prompt into Steem and add that the origin should be the closest configured store location to the customer's postcode.

The prompt

Loads into the composer so you can edit it first. Nothing is built, and nothing is charged, until you send it.

Build a WordPress plugin for WooCommerce that adds a distance-based local delivery shipping method. Register a custom WooCommerce shipping method with the ID local_delivery_distance and the title Local Delivery. In the method admin settings panel add: a Google Maps API Key text field, a Store Address text field used as the origin for distance calculations, and a custom HTML table for delivery tiers with three columns: minimum miles, maximum miles, and price in dollars. Include an Add Tier button to add rows and a Remove button on each row. Pre-populate the table with five default tiers: 0 to 5 miles at 3.00, 5 to 10 miles at 8.00, 10 to 15 miles at 14.00, 15 to 20 miles at 21.00, and 20 to 30 miles at 30.00. In the calculate_shipping method, build the destination address from the WooCommerce customer shipping fields, send a request to the Google Maps Distance Matrix API with the stored API key and store address as origin, parse the distance value in meters from the response and convert to miles, find the first matching tier where the distance falls between minimum and maximum, and call add_rate with the matched price and the method title. If no tier matches or the API request fails, add no rate so the method is unavailable at checkout.

Build this pluginAbout 55 credits · the free plan includes enough for one

Read next

Other plugins you can build this way

Each loads into the composer, ready to edit.