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 = GoogleDistanceName it anything you want really Handler = gmaps
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 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;
}
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.
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.
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.
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.
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.
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 = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins='+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;
}`