Velocity Coding Example - Display Item inventory conditional

Introduction

This tutorial shows how to use velocity code within your catalog templates to display an item's inventory and conditional .  This velocity code example is intended to be used with the catalog templates.

Sample Code

                 #if($item.isInventoryTracked())  
                      #if($item.getAvailableQuantity() <= 0)
                           Item is currently out of stock
                      #elseif ($item.getAvailableQuantity() >= 0)
			               Quantity in Stock: $item.getAvailableQuantity()
                      #end
                 #end

Breaking Down The Code

Here we are checking the item to see if inventory tracking has been turned on for the item. We do this to make sure that item is tracking inventory otherwise the quantity in stock is "blank" by default. which will output that the item is out of stock.

Main Condition
#if($item.isInventoryTracked())

This is our first condition, if the "AvailableQuantity" or Quantity in stock is 0 then we display the message below.

Condition statement 1
   #if($item.getAvailableQuantity() <= 0)
        Item is currently out of stock

This is our second condition, If the "AvailableQuantity" or Quantity in stock is greater then 0, then we display the message.

Condition statement 2
   #elseif ($item.getAvailableQuantity() >= 0)

Here we are looping thought the item to pull the Quantity in stock and then closing the conditional statements.

Output of Quantity in stock
        Quantity in Stock: $item.getAvailableQuantity()
    #end
#end