Advanced Tutorial: Automatic Delivery Charge Based on Distance via Google Maps


Here is the setup to charge the customer for delivery only if the distance is more than 10 km

Create required script from Automation > Scripts
Name = GoogleDistance Name it anything you want really
Handler = gmaps

function gdistance(origin,destination)


var nl = '<linebreak/>';

var u = ''+origin+'.&destinations='+destination+'.&mode=driving&language=en-GB&sensor=false&units=metric';

var alldata = web.Download(u);

var json = JSON.parse(alldata);

// distance.text gives data like 826 km
// var distance = json.rows[0].elements[0].distance.text;

// distance.value gives data like 826467 (which is meters, so divide by 1000 to get Km)
var distance = json.rows[0].elements[0].distance.value/1000;

return distance;


Make a new entity custom data field called Distance and PostCode could also be Address

Set an action to Update Entity Data

Set an action to Update Ticket Calculation for delivery charge

Set a rule to fire Update Entity Data when Order Added to Ticket

Event = Order Added to Ticket
Custom Constraint = {ENTITY DATA:Customer:Distance} Greater 10000

Put this in the Distance variable for the Update Entity Data distance action:
{CALL:gmaps.gdistance(‘YOUR ADDRESS’,’{ENTITY DATA:Customer:PostCode}’)}

Set a rule to fire Update Ticket Calculation action if the distance is more than 10000 meter away.

This is the result:

How to use API. (Google Calender API)
V5 Delivery Package minutes since Deliverer Assigned
[? Question dropdown
Distance Field not updating correctly

Brilliant tutorial, thank you. Also great to see South Shields up there having it’s 15mins of fame, he he.


Well I cant take credit for the tutorial it was actually written by @pizzaeilat4 during the beta I converted it to a tutorial to share with everyone.


Is there anyway to do a fixed price instead of a percentage calculation? It seems the Update Ticket Calculation action can only do a percentage. Unless there is another action but i couldn’t find any.

For example, charging $1 per km or adding a fixed price of $5 to all deliveries over 1km.



Change the Calculation Type to use Fixed amount


I think always use Fix Amount and calculate the amount by the script is easier. For me, I prefer to edit script than Rules/Action if you need to change how you charge the delivery fee.


Thanks kendash. I’m learning something new about samba each day and slowly building up my knowledge, brillant!

@sukasem - Would love to, but I think that is still beyond me!


It is easy.

function deliveryFee(origin,destination)

	var u = ''+origin+'.&destinations='+destination+'.&mode=driving&language=en-US&sensor=false&units=metric';
	var alldata = web.Download(u);
	var json = JSON.parse(alldata);
	var distance = json.rows[0].elements[0].distance.value;
	if (distance < 100) {
		var fee = 0;
	if (distance >= 100 && distance < 1600) {
		var fee = 5;
	if (distance >= 1600 && distance < 3100) {
		var fee = 6;
	if (distance >= 3100 && distance < 6100) {
		var fee = 7;
	if (distance >= 6100 && distance < 8100) {
		var fee = 8;
	if (distance >= 8100) {
		var fee = -1;
	return fee;

V5 Delivery Package minutes since Deliverer Assigned

Right, I’ve followed the tutorial and unfortunately I can’t get this working. I’ve managed to add the distance and that part is fine, but there is no ticket calculation update on the ticket.
I’ve looked through my actions/rules all last night but can’t seem to find where i’ve made the mistake. I assume it is to do with my calculation type/calculation action.

Would it possible for someone to glance through and try and find what error I’ve made please? I think it must just be a little one, but no matter how many times I check my rules over, I can’t see anything wrong :sweat:

Picture of the ticket - distance updates fine, but no calculation/charge is shown on the ticket and rules below.


Add popup message action to delivery charge 2 rule to see if the rule was actually fired.



Added a message popup and still nothing happened - no calculation and no message.

Then I tried removing the constraint of {ENTITY DATA:Customer:Distance} and keeping the actions. This just results in samba crashing everytime I make an order.

Then I removed the constaint, removed the delivery charge calculation action and just kept a show message action. (i.e. order added to ticket fires a message only) and that works. So is the problem with the constraint?


UPDATE: I think i’ve narrowed the problem to the {ENTITY DATA:Customer:Distance} constraint. I added <L00> ENTITY DATA:Customer:Distance} to my ticket lister to see if it is working, and it shows the distance on the lister fine.

I then removed the constraint from my rule completely, as mentioned, it kept crashing sambapos everytime I added an order.

Next, I changed the constraint to {TICKET TOTAL} greater than 1}. This is now firing the rule perfectly and everytime I make a ticket with over £1 it calculated a delivery charge!

Problem is that I need the delivery charged to be based on the Km not the ticket total.

Screenshot below.


Popup message is just like debugger. Add popup message and type any message in the message box. If it actually popup then the rule was fired but some thing wrong with delivery charge action.

If no popup when you add order then, the rule was not trigger.


The rule is refusing to trigger when I use the constraint `{ENTITY DATA:Customer:Distance} greater than X
Although this syntax displays fine on my ticket lister.
The rule is triggering fine when I change theconstraint to {TICKET TOTAL}

Update: I’ve tried changing the constraint to {ENTITY DATA:Customer:Distance} IS NOT NULL and this triggers fine. It seems to not recognise the constraint of “greater than X” where X = the distance value (e.g. 1, 1 km or 1km - tried all 3)

Update 2: Syntax of {ENTITY DATA:Customer:Distance} IS LESS THAN X triggers fine…still cant get Greater than X to fire*

Update 3: The {ENTITY DATA:Customer:Distance} IS LESS THAN X seems to be triggering everytime, regardless of the distance.


Because you distance is text not number I guess.


But the {ENTITY DATA:Customer:Distance} works with IS NOT NULL and LESS and the rule fires.


Text is not null and it has 0 value.
Try change

var distance =json.rows[0].elements[0].distance.text to

var distance = json.rows[0].elements[0].distance.value


Right, tried editing the script, still doesn’t work, no message or calculation.

Tried the constraint {ENTITYDATA:Customer:Distance} `GREATER than X`` - where X was 0, 1, 100,500,1000

Also tried the new edited script with {ENTITY DATA:Customer:Distance} LESS THAN 50000 and that worked perfectly.

Note, this is the script in case it identifies any problems:

``function gdistance(origin,destination)`


    var nl = '<linebreak/>';

    var u = ''+origin+'.&destinations='+destination+'.&mode=driving&language=en-GB&sensor=false&units=metric';

    var alldata = web.Download(u);

    var json = JSON.parse(alldata);

    var distance = json.rows[0].elements[0].distance.value;

    return distance;



Do you create custom field name “Data”?


Sorry that was a typo, meant to say {ENTITY DATA:Customer:Distance} on all my posts. Have edited them now.