Developer forum

Forum » Development » Show product discount name in sales badge

Show product discount name in sales badge

Caro De Weze
Reply

Hi,

I want to show the name of the product discount in Swift's ecommerce sales badge instead of the percentage (see attachment). I'm quite new to this, so can someone tell me which file I need to edit (I think Swift_EcommerceBadge.cshtml) and how I load the new variable discount name instead of the percentage?

Caro


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Caro

In Swift/Components/EcommerceBadge.cshtml you have the below code - I added some very untested code, but it will get you the name of the (first) discount applied - there can be many, and this one will take the first. You might want to add more logic to when to override the badge name and add additional null checks

@if (Model.Discount.Price != 0)

{
    string saleBadgeCssClassName = GetViewParameter("saleBadgeCssClassName") != null ? GetViewParameterString("saleBadgeCssClassName").Trim().ToLower() : "";
 bool saleBadgeEnabled = saleBadgeCssClassName != "" && saleBadgeCssClassName != "none" ? true : false;

 if (saleBadgeEnabled)
 {

 string saleBadgeType = GetViewParameter("saleBadgeType") != null ? GetViewParameterString("saleBadgeType") : "amount";
 string title = saleBadgeType == "amount" ? "-" + Model.Discount.PriceFormatted : "";
 title = saleBadgeType == "percentage" ? "-" + Math.Round((Model.Discount.Price / Model.PriceBeforeDiscount.Price) * 100) + "%" : title;

        var discountName = Model.ProductDiscounts.First().Name;
        title = discountName;

 string hideAutoLabel = "";
 if (saleBadgeType == "amount" || saleBadgeType == "percentage")
 {
 hideAutoLabel = "badge-no-label";
 }
 <span class="@sizeCssClass"><span class="badge @hideAutoLabel @(saleBadgeCssClassName) rounded-0">@title</span></span>
 }
}
Votes for this answer: 2

 

You must be logged in to post in the forum