The data stored within the orders collection can be referenced and used within any message body.
Our Example
For our example we'll be using hypothetical order data. In order to test in your system you will need to add your own order data via the API. Learn more about order data.
Using the tabs below you can view the HTML and Smarty used in the message, the order data being referenced (in JSON format) and the rendered output that will display when a message is sent or previewed.
{$orders=$utils->getOrders()}
{foreach $orders as $order}
<b>OrderID {$order.orderID}</b><br>
Purchase Date - {$order.purchaseDate|date_format}<br><br>
{foreach $order.items as $orderitem}
Product {$orderitem.productID}<br>
{$orderitem.description|capitalize} - ${$orderitem.amount|string_format:"%.2f"}<br>
Size - {$orderitem.attr.size}<br>
Color - {$orderitem.attr.color}<br><br>
{/foreach}
Total Amount - ${$order.totalAmount}<br><br>
Shipping Address:<br>
{$order.shippingAddress.name|capitalize}<br>
{$order.shippingAddress.address|capitalize}<br>
{$order.shippingAddress.city|capitalize} {$order.shippingAddress.state|capitalize} {$order.shippingAddress.postalCode}<br><br>
{/foreach}
[ { "cID": "58d2fc99ac0c8117814d4e78" "orderID": "001", "purchaseDate": "2015-01-10T01:47:43+0000", "shippingAddress": { "name": "fred garvin", "address": "555 truss St", "city": "san diego", "state": "ca", "postalCode": "92108", "country": "usa" }, "billingAddress": { "name": "fred garvin", "address": "555 truss st", "city": "san diego", "state": "ca", "postalCode": "92108", "country": "usa" }, "items": [ { "productID": "111", "description": "khaki shorts", "sku": "1212", "category": "shorts", "name": "khaki shorts", "qty": 1, "itemPrice": 19.99, "amount": 19.99, "attr": { "color": "tan", "size": "large" } }, { "productID": "222", "description": "khaki pants", "sku": "1213", "category": "pants", "name": "khaki pants", "qty": 1, "itemPrice": 29.99, "amount": 29.99, "attr": { "color": "tan", "size": "large" } } ], "tax": 0, "totalAmount": 49.98 } ]
Purchase Date - Jan 10, 2015
Product 111
Khaki Shorts - $19.99
Size - large
Color - tan
Product 222
Khaki Pants - $29.99
Size - large
Color - tan
Total Amount - $49.98
Shipping Address:
Fred Garvin
555 Truss St
San Diego CA 92108
{$orders=$utils->getOrders()} {$utils->jsonPrettyPrint($orders)}
The getOrders Method
In order to access order data within a message, use the getOrders method, which is a custom extension of Smarty provided by Cordial.
The getOrders method has 3 parameters: query, limit and sort. They must be written in this order and separated by commas. All are optional.
getOrders($query, $limit, $sort)
Parameters | Description | Required | Expected | Default |
---|---|---|---|---|
query | A filter used to limit the data set. | Optional | Array | [] |
limit | A filter that sets a maximum number of records to return. | Optional | Integer | 25 |
sort | Sort the order of the records returned. | Optional | Array | [] |
How to write the getOrders method
{$orders=$utils->getOrders()}
In the example above, a variable "orders" is assigned to the resulting data set returned from the orders collection. The getOrders method will get the data from the orders collection for each message that is rendered per contact.
How to set the query
In some cases, you may want to get specific records from the order collection. You can achieve this by adding query filters.
Return records filtered by a specified value.
["key"=>"value"]
Example:
{$orders=$utils->getOrders(["orderID"=>"002"])}
Return records filtered by multiple specified values.
["key"=>"value", "key2"=>"value2"]
Example:
{$orders=$utils->getOrders(["purchaseDate"=>["gt"=>"2016-01-09"],"shippingAddress.city"=>"san diego"])}
Return records filtered by a value within an order item.
["items"=>["key"=>"value"]]
Example:
{$orders=$utils->getOrders(["items"=>["productID"=>"111"]])}
How to set the limit
Use the limit filter to limit the amount of records returned.
Example with no query filter and a limit of 5 records:
{$orders=$utils->getOrders([],5)}
Example with a query filter and a limit of 5 records:
{$orders=$utils->getOrders(["purchaseDate"=>["gt"=>"2016-01-09"]],5)}
How to sort the data
Sort results by purchase date in an ascending order.
{$orders=$utils->getOrders([],null,['column'=>'purchaseDate','direction'=>'ASC'])}
Sort results by purchase date in an descending order.
{$orders=$utils->getOrders([],null,['column'=>'purchaseDate','direction'=>'DESC'])}
Sort results by order ID in an descending order.
{$orders=$utils->getOrders([],null,['column'=>orderID,'direction'=>'DESC'])}
Rendering the data in a message
After the data is available with the getRecords method, you can render the data in the message.
Looping through data with {foreach}
A {foreach} statement is the most common way to loop through the data array and render the results in a message.
Example:
{$orders=$utils->getOrders()}
{foreach $orders as $order}
<b>OrderID {$order.orderID}</b><br>
Purchase Date - {$order.purchaseDate|date_format}<br><br>
{foreach $order.items as $orderitem}
Product {$orderitem.productID}<br>
{$orderitem.description|capitalize} - ${$orderitem.amount|string_format:"%.2f"}<br>
Size - {$orderitem.attr.size}<br>
Color - {$orderitem.attr.color}<br><br>
{/foreach}
Total Amount - ${$order.totalAmount|string_format:"%.2f"}<br><br>
Shipping Address:<br>
{$order.shippingAddress.name|capitalize}<br>
{$order.shippingAddress.address|capitalize}<br>
{$order.shippingAddress.city|capitalize} {$order.shippingAddress.state|capitalize} {$order.shippingAddress.postalCode}<br><br>
{/foreach}
Note that order items are stored as an array. You can use a nested {foreach} in order to loop through each of the items per order.
Learn more about using the {foreach} statement.
Render orders between two purchase dates
You can render orders placed between two specified dates. This method requires the use of getConvertedDate Smarty utility.
{$orders.startDate = '2018-01-22'} {$orders.endDate = '2018-01-22'} {$orders.query.purchaseDate.between.start = $utils->getConvertedDate($orders.startDate|cat:' 00:00:00', 'UTC', $account.timezone, 'Y-m-d\TH:i:sO')} {$orders.query.purchaseDate.between.end = $utils->getConvertedDate($orders.startDate|cat:' 23:59:59', 'UTC', $account.timezone, 'Y-m-d\TH:i:sO')} {$orders.limit = 50} {$orders.sort = ['column'=>'purchaseDate','direction'=>'DESC']} {$orders.data = $supplements->getOrders($orders.query, $orders.limit, $orders.sort)} <h2>Debug $orders</h2> {$utils->jsonPrettyPrint($orders)} <h2>Debug $contact</h2> {$utils->jsonPrettyPrint($contact)}
Referencing an index of the array
You also have to option to target the "nth" item within an array using it's index. For example, if you only wanted to render the first item of each item array you would write:
{$orders=$utils->getOrders()}
{foreach $orders as $order}
<b>OrderID {$order.orderID}</b><br>
Purchase Date - {$order.purchaseDate|date_format}<br><br>
Product {$order.items[0].productID}<br>
{$order.items[0].description|capitalize} - ${$order.items[0].amount|string_format:"%.2f"}<br>
Size - {$order.items[0].attr.size}<br>
Color - {$order.items[0].attr.color}<br><br>
Total Amount - ${$order.totalAmount|string_format:"%.2f"}<br><br>
Shipping Address:<br>
{$order.shippingAddress.name|capitalize}<br>
{$order.shippingAddress.address|capitalize}<br>
{$order.shippingAddress.city|capitalize} {$order.shippingAddress.state|capitalize} {$order.shippingAddress.postalCode}<br><br>
{/foreach}
This example will look in the 1st item of the items array and render the values for "productID", "description", "amount", "size" and "color".
In the next article learn about getting event data.
Comments
0 comments
Please sign in to leave a comment.