Conversion Pixel Item Loop in Velocity

Conversion Pixel Item Loop in Velocity

Introduction

This short tutorial will explain how to use velocity to output item information for a conversion pixel. Let's say that our pixel looks like this:

<!-- Listrak Conversion Tracking -->
<script type="text/javascript">

    _ltk.Order.SetCustomer('[Email]', '[BillingFirstName]', '[BillingLastName]');

    _ltk.Order.OrderNumber = '[OrderId]';

    _ltk.Order.ItemTotal = '[Subtotal]';

    _ltk.Order.ShippingTotal = '[Shipping]';

    _ltk.Order.TaxTotal = '[Tax]';

    _ltk.Order.HandlingTotal = '0.00';

    _ltk.Order.OrderTotal = '[Total]';

    _ltk.Order.AddItem('Item1', 1, '5.00');// one line per item - Item ID, Qty, Subtotal

    _ltk.Order.Submit();

</script>

<!-- Listrak Conversion Tracking -->

The line that is not possible to do with simple tokens and requires velocity is:

    _ltk.Order.AddItem('Item1', 1, '5.00'); // one line per item - Item ID, Qty, Subtotal

Here is the velocity code to perform this loop:

#foreach ($item in $order.getItems())
  _ltk.Order.AddItem('${item.getItemId()}', ${item.getQuantity()}, '${item.getCost()}');
#end

And here is the velocity code inside the larger snippet of javascript:

<!-- Listrak Conversion Tracking -->
<script type="text/javascript">

    _ltk.Order.SetCustomer('[Email]', '[BillingFirstName]', '[BillingLastName]');

    _ltk.Order.OrderNumber = '[OrderId]';

    _ltk.Order.ItemTotal = '[Subtotal]';

    _ltk.Order.ShippingTotal = '[Shipping]';

    _ltk.Order.TaxTotal = '[Tax]';

    _ltk.Order.HandlingTotal = '0.00';

    _ltk.Order.OrderTotal = '[Total]';

#foreach ($item in $order.getItems())
  _ltk.Order.AddItem('${item.getItemId()}', ${item.getQuantity()}, '${item.getCost()}');
#end

    _ltk.Order.Submit();

</script>

<!-- Listrak Conversion Tracking -->