Tips and cases of use for a successful WordPress website with calculated forms.
How to determine the price of a service that is based on the distance between addresses.
The "Calculated Fields Form" plugin is very versatile, it includes a large number of operations that allow to implement a wide range of projects.
In this article will be described a specific case of use: How to determine the price of a service that is based on the distance between addresses.
Assuming it is a moving company, where the price is estimated based on the miles from the origin address to destination, multiplied by a factor determined by the company (for this article will be assumed the factor is $1.05 per mile).
In a real moving company this form would include many other fields: contact information, the number of boxes, the floor, etc. But for simplicity we'll focus on those fields required to get the distance and price based on this distance.
The basic fields in the form would be:
This project requires to determine the distance between the addresses entered by the users. To facilitate this task the plugin includes the DISTANCE Operations Module distributed with the Developer and Platinum versions of the plugin, in particular the DISTANCE operation.
The protocol of the DISTANCE operation is:
DISTANCE(origin address, destination address, unit system, travel mode)
The possible unit system are: km (for kilometers), and mi (for miles) The possible travel modes are: DRIVING, BICYCLING, TRANSIT, WALKING, or STRAIGHT
With this information the equation associated to the calculated field would be:
PREC(DISTANCE(fieldname1, fieldname2, 'mi', 'DRIVING')*1.05, 2)
The PREC(X, Y) rounds the number X with Y decimal places. In the previous equation, as it is a currency result, it is being rounded to 2 decimal places.
Pay attention: The DISTANCE operation is based on the Google Maps APIs, so, you need an API Key for using it, furthermore, in your Google Maps Project should be enabled the following APIs:
The Google Maps API Key is passed to the form through its shortcode, assuming the form id is 1, and the API Key: 1234567890, the shortcode would be:
[CP_CALCULATED_FIELDS id="1" google_api_key="1234567890"]
Entering the origin and destination addresses in a format understood by Google might be difficult in some cases, reduce the friction can make the difference between an awful or pleasurable user's experience.
To improve the user experience, it is possible to enable the Google Places add-on distributed with the Platinum version of the plugin. The Google Places add-on allows to transform some fields in the form into auto-complete address fields, recommending to the user a list of possible addresses based on the partial address typed by him.
The first step would be enable the add-on from the settings page of the plugin.
The previous action will display a new section in the settings page of the plugin for entering the Google API Key, generated previously in the Google website (for using Google Places in the project, it is required to enable the Places API on Google)
Once enabled the add-on, and entered the API Key, is time to configure the form:
Populating the origin address
Populating the destination address
Equation's result