Distance-based delivery pricing in WooCommerce

Published Updated 2 min read

WooCommerce comes with flat rate, free shipping and local pickup, and plugins add pricing by weight, item count or order total. None of them can charge by how far the driver has to go.

For a florist, a bakery or anyone delivering within a fixed area, distance is the cost. A tier table answers it: up to five miles costs one amount, up to ten costs more, and so on. A small plugin can connect that table to the Google Distance Matrix API and offer it as a proper WooCommerce shipping method that prices itself from the customer's address.

Every rate is worked out from the cart, not the address

WooCommerce's shipping options and most third-party plugins calculate from what is in the cart: weight, dimensions, item count, order value. That suits carriers who price on the same variables.

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

A Local Delivery method with a tier table

The plugin registers a shipping method called Local Delivery. Add it to a shipping zone like any other method, so the zone decides which regions see it at all, then set a Google Maps API key, your store address and the distance tiers in its settings.

Each tier has a minimum and maximum distance in miles and a price. Five default rows cover zero to thirty miles, and you can add, remove or change rows at any time; changes apply from the next checkout.

When a customer enters a shipping address at checkout, the plugin sends your address and theirs to the Distance Matrix API, reads back the driving distance, finds the matching tier and returns its price as the shipping cost. An address beyond every tier makes Local Delivery unavailable, and the customer can still check out with any other method you offer.

Getting a Google Maps API key

In the Google Cloud Console, create or choose a project and enable the Distance Matrix API. Under Credentials, create an API key and restrict it to the Distance Matrix API, adding an IP restriction too if your server has a fixed address.

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

Hard boundaries, kilometres and traffic

An address outside your tiers simply loses the option. If you want to refuse orders beyond a radius, with a message saying why, that is a different job.

Kilometres, traffic-aware routing and links to third-party logistics services are where the paid options come in. This build calculates one thing: the delivery price from the customer's distance to one store address. A follow-up message can use the nearest of several store locations as the starting point instead.

What you get

Included

  • 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

Not included

  • 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 the things on the right. This build covers the part most sites use.

Questions

Does the Google Distance Matrix API cost money?

Yes, though very little for a small local delivery store. Google includes a monthly credit across Maps Platform products, and typical local delivery volumes are unlikely to exceed it. Check Google Cloud pricing for current rates.

Build it in Steem

The button opens the builder with this guide's prompt filled in, so you can change anything before it runs. Nothing is built, and nothing is charged, until you send it.

Build this pluginAbout 55 credits. The free plan includes enough for one.
Read the prompt (217 words)

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.