Overview
Using the custom Smarty utility distanceBetweenPostalCodes
, you can calculate the distance between two postal codes and render content based on that distance. You can use this utility to display dynamic content according to where someone lives.
{$utils->distanceBetweenPostalCodes($zip1, $zip2, $units, $countryZip1, $countryZip2)}
Parameters | Description | Required | Expected | Default |
---|---|---|---|---|
zip1 |
A valid 5 digit US postal code | Required | Integer | NA |
zip2 |
A valid 5 digit US postal code | Required | Integer | NA |
units |
Unit of measurement (miles or kilometers) | Optional | String | miles |
countryZip1 |
A valid 2 digit country code | Optional | String | US |
countryZip2 |
A valid 2 digit country code | Optional | String | US |
Calculate distance using set postal codes
-
{$distance = $utils->distanceBetweenPostalCodes(35244,92109)} {if $distance !== 0} Distance = {$distance} {/if}
-
Distance 1 = 1756.88
In the above example, we're setting a variable called distance
and running the Smarty utility to calculate the distance between the two set postal codes (35244 and 92109). We can then print the result in a message using the Smarty code: {$distance}
.
To test the Smarty output in your Cordial account, paste the above code into your message HTML or Sculpt Block and click the Preview button.
Check for 0 with a triple equal. When the function returns 0, it means it could not calculate. However, the string 0.00 means zero miles between points.
Display content based on distance to a contact's postal code
{$distance = $utils->distanceBetweenPostalCodes({$contact.address.postal_code},90001)} {if $distance < 200 && $distance !== 0} Marketing content {else} Default content {/if}
The above code will display Marketing content
where the contact's postal code is less than 200 miles from 90001. The contact's postal code can be pulled from any contact attribute.
Use case
There's a cold front in Los Angeles, so you want to show your new collection of jackets to people within 200 miles of that city.
Display nearest location to a contact's postal code
People love to know what store is closest to them. The following code builds upon the distanceBetweenPostalCodes
to do exactly that.
-
{$locations=$utils->getJson("https://p5.zdassets.com/hc/theme_assets/656597/200049057/store_locations.json")}
{foreach $locations as $location}
{$distance = $utils->distanceBetweenPostalCodes({$contact.address.postal_code},{$location.ZIP})}
{$tempArray[] = [
'distance' => {$distance},
'address' => {$location.Address},
'name' => {$location.Name},
'state' => {$location.State},
'zip' => {$location.ZIP}
]}
{/foreach}
<!--Sort Locations by Distance-->
{$sorted_by_distance = $utils->sortArray($tempArray, 'distance', 'asc')}
<!--Print Locations-->
<strong>Nearest Location</strong><br><br>
{foreach $sorted_by_distance as $closest}
{$closest.name}<br />
{$closest.address}<br />
{$closest.state} {$closest.zip}<br />
Approx {$closest.distance|string_format:"%d"} miles<br /><br />
{/foreach} -
[{ "Address":"3430 Galleria Circle", "Name":"Birmingham", "State":"Alabama", "ZIP":"35244" }, { "Address":"11525 Cantrell Rd", "Name":"Little Rock", "State":"ARKANSAS", "ZIP":"72212" }, { "Address":"3040 El Camino Real", "Name":"Tustin", "State":"CALIFORNIA", "ZIP":"92782" }, { "Address":"24201 Valencia Blvd Suite 3210", "Name":"Valencia", "State":"CALIFORNIA", "ZIP":"91355" }, { "Address":"5102 North Nevada Avenue", "Name":"Colorado Springs", "State":"COLORADO", "ZIP":"80918" }, { "Address":"8100 W Crestline Ave", "Name":"Littleton", "State":"COLORADO", "ZIP":"80123" }, { "Address":"4920 Thompson Pkwy", "Name":"Loveland", "State":"COLORADO", "ZIP":"80534" }, { "Address":"10438 Towncenter Drive", "Name":"Westminster", "State":"COLORADO", "ZIP":"80021" }, { "Address":"18713 Biscayne Boulevard", "Name":"Aventura", "State":"FLORIDA", "ZIP":"33180" }]
-
Nearest Location
Birmingham
3430 Galleria Circle
ALABAMA 35244
Approx 4 miles
Tustin
3040 El Camino Real
CALIFORNIA 92782
Approx 79 miles
Valencia
24201 Valencia Blvd Suite 3210
CALIFORNIA 91355
Approx 143 miles
Colorado Springs
5102 North Nevada Avenue
COLORADO 80918
Approx 805 miles
Littleton
8100 W Crestline Ave
COLORADO 80123
Approx 816 miles
Westminster
10438 Towncenter Drive
COLORADO 80021
Approx 824 miles
Loveland
4920 Thompson Pkwy
COLORADO 80534
Approx 849 miles
Little Rock
11525 Cantrell Rd
ARKANSAS 72212
Approx 1417 miles
Aventura
18713 Biscayne Boulevard
FLORIDA 33180
Approx 2260 miles
In the example above, we're rendering a list of stores that's sorted by the nearest location to the contact. We achieve this using several functions of Smarty:
- Retrieve a list of stores with the getJson method.
- Loop through each store location and create a temporary array of the results (including the calculated distance of each store to the contact's postal code).
- Sort the temporary array by distance in an ascending order.
- Render the results in the message.
We can also add some optional filters:
- Display the three closest results.
- Display alternative text if the nearest location is more than 50 miles away.
Retrieve a list of stores with the getJson method
{$locations=$utils->getJson("https://p5.zdassets.com/hc/theme_assets/656597/200049057/store_locations.json")}
The above Smarty code will pull a list of stores from a remotely hosted JSON file. You can use this file in your account as an example for testing purposes.
Loop through each store location and create a temporary array of the results
{foreach $locations as $location} {$distance = $utils->distanceBetweenPostalCodes({$contact.address.postal_code},{$location.ZIP})} {$tempArray[] = [ 'distance' => {$distance}, 'address' => {$location.Address}, 'name' => {$location.Name}, 'state' => {$location.State}, 'zip' => {$location.ZIP} ]} {/foreach}
The above code uses foreach
to loop through each of the store locations in the JSON file. Then distanceBetweenPostalCodes
is used to get the distance of each location to the contact's postal code ($contact.address.postal_code
). Finally, a temporary array is created to include the distance in the list of locations.
Sort the temporary array by distance in an ascending order
{$sorted_by_distance = $utils->sortArray($tempArray, 'distance', 'asc')}
The above code uses the sortArray
utility to sort the temporary array according the calculated distance.
Render the results in the message
<strong>Nearest Location</strong><br><br>
{foreach $sorted_by_distance as $closest}
{$closest.name}<br />
{$closest.address}<br />
{$closest.state} {$closest.zip}<br />
Approx {$closest.distance|string_format:"%d"} miles<br /><br />
{/foreach}
The above code will then render the sorted array of locations in the message and display the distance of each location to the contact.
Display the three closest results
{if $closest@iteration > 3}{break}{/if}
Using @iteration
, we can display only the top three closest results in the message. The code above is placed within the foreach loop and will break the loop after three results.
Display alternative text if the nearest location is more than 50 miles away
{if $closest.distance > 50}<p>There are no locations within 50 miles of your zip code.</p>{break}{/if}
The above code will display alternative text if the contact's zip code is more than 50 miles from the nearest store location.
In the next article, you can learn how to sort an array.
Comments
0 comments
Please sign in to leave a comment.