Velocity Coding Example - Receipt Conditional Text From Item Attribute

Introduction

This tutorial shows how to use Velocity code on your receipt screen branding to conditionally show some content when an item contains a certain attribute value.  This velocity code example is intended to be used on the Receipt screen branding located:

 

Main Menu Configuration  Screen Branding Themes Screens Receipt

The order object used in this example is only available in the receipt.  It is not available in the catalog or checkout screens prior to the receipt.

 

Sample Code

#set($showForm = false)
#foreach ($item in $order.getItems())
  #if ($item.getAttributeValue("RxAuth") == "y")
    #set($showForm = true)    
  #end
#end
#if ($showForm)
  <!-- Conditional HTML Here --> 
#end

Break Down

Now let's break down what each block is doing.

#set($showForm = false)

This line initializes a new variable named showForm which we will check later.

#foreach ($item in $order.getItems())
  #if ($item.getAttributeValue("RxAuth") == "y")
    #set($showForm = true)    
  #end
#end

Here we have looped through the items on an order.  For each item we have checked an attribute named "RxAuth" to see if the value of the attribute is "y".  If the value of the attribute is "y" then we change showForm to true.

#if ($showForm)
  <!-- Conditional HTML Here --> 
#end

If showForm is true then we output the HTML that is between the #if and #end tags to the browser.