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

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.

3 Likes

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.

Regards

Change the Calculation Type to use Fixed amount

2 Likes

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.

1 Like

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 = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins='+origin+'.&destinations='+destination+'.&mode=driving&language=en-US&sensor=false&units=metric';
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;
}``````
2 Likes

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

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.

Ok,

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

1 Like

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

Tried the constraint `{ENTITY`DATA: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 = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins='+origin+'.&destinations='+destination+'.&mode=driving&language=en-GB&sensor=false&units=metric';

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.

Try this constraint:

``````[=TN('{ENTITY DATA:Customer:Distance}')]  Greater  1
``````

To be clear, the better one to use is the `Show Message` Action, not `Display Popup` Action.

1 Like

Nope, unfortunately that hasn’t triggered anything. (I’ve been using the `Show Message Action`)

Out of interested I tried `[=TN('{ENTITY DATA:Customer:Distance}')] Less 1` - This triggered on everything, regardless of distance