Adding a sale banner
In this guide you will learn how to add a sale banner showing the sale percentage to your product listings by customizing the result template.
Last updated
In this guide you will learn how to add a sale banner showing the sale percentage to your product listings by customizing the result template.
Last updated
In this guide you will learn how to add a sale banner showing the sale percentage to your product listings by customizing the result template.
Basic knowledge of HTML and CSS.
Familiarity with the Tempura template language
To get started,
Open the Search UI Designer
Select the Custom option in the Search result template section
Click Edit
In this guide we will learn how to add a banner to all products that are on sale.
Topics covered in this guide
Using "if" statements
Checking the product status
Editing the style sheets
Getting started
Select the Default template from the list of available templates. We use this template as a starting point.
We want to add a sales banner that only shows when a product is on sale. To do that, we will add a conditional block, using an if statement in Tempura. Add the following code underneath the <div class="item__image-wrapper">
tag.
In the #if
statement we check the productStatus.onSale
attribute, which returns true
if the product is on sale and false
if it is not on sale. As a result, the contained div
will only show if the product is on sale.
You might have noticed that we gave the div
a class name called sale-banner
. Let's use that class name to style this block. Click on the CSS tab to access the stylesheet for this template.
At the end of the CSS file, add the following code.
We are positioning the div
on top of the image in the top left corner. By giving it a background and a prominent text color, we have created an attention grabbing banner.
💡 The preview might not show the banner if the product is not on sale. Negate or remove the if statement above temporarily to see what it would look like.
The final result is shown below. All items that are on sale now have a banner in the top left, drawing attention to the sale item.
Showing that an item is on sale is great, but it's even better to show the actual discount percentage. Since the percentage might vary depending on the variant prices, we will calculate the largest discount of any variant and display that number.
Topics covered in this guide
Creating variables
Using "each" loops to iterate over Array fields
Using Math functions for calculations
Getting started
Taking the HTML code
Lastly we replace the static "On Sale" text from the previous guide with the discount
variable we used to calculate the percentage.
The final result shows the automatically calculated discount percentage as a banner in the top left of the sale item. A simple but elegant way to draw attention to the sale while communicating the discount percentage to the customer.
Click to see the full template code