Rules distance calculator for Drupal Commerce

Events happening in the community are now at Drupal community events on www.drupal.org.
joecanti's picture

Hi,

So for Drupal Commerce, Rules is used for calculating shipping.

In my case, I want to calculate shipping based on distance...EG:

0 - 25 miles FREE
25 - 50 miles £10
50 - 100 miles £25

This would require that rules takes the customer postcode, and compares it to my postcode using Google Maps to find out the distance, and then spits out the price.

The problem is I have no idea how to do this in Rules! So any help would be very much appreciated.

Magento Cart have something similar as a plugin, but I've seen nothing for any other ecommerce systems.

Many thanks, Joe

Comments

Hi Joe, I don't think you

franperr's picture

Hi Joe,
I don't think you need to use google maps to do it.
There is a free (as in free beer, I don't know if it's open source) database called geonames that contains a lot of place names, all around the world, with the coordinates for each place (may be it contains the postal code too).
Once you have the place coordinates, you can easily calculate the distance between 2 points on Earth (it's not totally accurate, there is a margin of error around 5%, but I think it's easier than using google maps api).

Hi, Thanks Franperr -

joecanti's picture

Hi,

Thanks Franperr - unfortunately the geonames service doesn't really do postal codes. There are databases available for post code co-ordinates though, but I thought it would be easier to use Google to calculate on the fly rather than manage a huge database.

Having said that I don't really want to rely on Google for my ecommerce shipping calculations - because if it isn't available, it could cause problems.

I could perhaps make things less accurate and just use the outward code - there are only 3,000 of those in the UK as opposed to 2 million full codes.

I could theoretically enter these into rules, so that Rules can determine which postal band the buyers address fits into. But compiling that data, with the distance from the office address - is no small task!

I don't know. At the moment I have a manual distance checker, and then several bands for the customer to choose from - but it's all a bit clunky, and the distance checker often doesn't work well.

Thanks for your reply, Joe

Sorry to hear that geonames

franperr's picture

Sorry to hear that geonames doesn't have postal codes for your area.
If you can reduce to only 3000 different possibilities, I think it would be workable to create a new table in the database to hold this information, and then use a custom php snippet or module that you can call from rules to do the calculation.
Doing a quick search, I found this, may be it's useful: http://www.freemaptools.com/download-uk-postcode-lat-lng.htm

Thanks for the link - that's

joecanti's picture

Thanks for the link - that's really useful. Would you know how I can calculate the distance of each of those postcodes from our office postcodes automatically?

It would be great to have a third column in the Excel file - 'distance from office'

But a little labourious to do each one separately!

Many thanks, Joe

Hi Joe, You can do it on the

franperr's picture

Hi Joe,
You can do it on the fly each time, or of course save it directly in the table as your office is not moving very often, I suppose ;-)
I used in the past this information, it shows you different method to calculate the distance between 2 points on earth: http://www.movable-type.co.uk/scripts/latlong.html
I'm sure you can adapt it to set a formula on Excel...

Ok thanks. I'm getting there

joecanti's picture

Ok thanks. I'm getting there now...

The equation was the tricky thing...

After some Googling I found this page:

http://bluemm.blogspot.de/2007/01/excel-formula-to-calculate-distance.ht...

Which gives the following formula:

=ACOS(COS(RADIANS(90-Lat1)) *COS(RADIANS(90-Lat2)) +SIN(RADIANS(90-Lat1)) *SIN(RADIANS(90-Lat2)) *COS(RADIANS(Long1-Long2))) *6371

Which gives a km measurement between 2 lat/long values.

My postage calculator won't be that accurate - but we're already using 'as the crow flies' anyway, so I don't think it'll be too bad.

Thanks very much for your help Franperr - much appreciated.

We'll see how this goes now.

Thanks, joe

Calculation via Google or Location-Module

drupa11y's picture

Just found that solution for this: http://drupalgarden.blogspot.de/2013/06/calculate-distance-between-posta...

Another solution base is the "location"-module which has ZIP & Geocodes:
http://drupal.stackexchange.com/questions/30088/location-distance-based-...

or the Adressfield-Module with geocoder & geofield: https://drupal.org/node/1944648

Thanks. I'd like to use the

joecanti's picture

Thanks. I'd like to use the Geofield / geocoder combination because you can use Google maps and yahoo - so if one gives no response, you have a fall back option.

I had a look at those posts, but getting location to work with rules might be a bit beyond me. For now, I'm going to stick with the outcodes - and in the future, I think I'll go across to something involving an automatic lookup.

cheers! Joe

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: