Display the Variant discount amount

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 12 ans
I would like to display the discount amount or name of a variant on a product page. Where do I find this in the API ??

I've got this far in the CatalogController.cs:


else
                                    {
                                        model.OldPrice = null;
                                        model.Price = _priceFormatter.FormatPrice(finalPrice);

                                        if (productVariant.AppliedDiscounts.Count() > 0)
                                        {
                                            model.DiscountName = "20% Off";
                                            // The above line is only for testing but will only show for products with a variant that has a discount applied.
                                            // Need to replace the static text with the discount name or value
                                        }
                                        
                                    }
Il y a 12 ans
productVariant.AppliedDiscounts will be a list of Discounts so you need to loop over it and get all the names because there could be more than one.

Untested:

string discountNames = "";
foreach(Discount discount in productVariant.AppliedDiscounts)
{
     discountNames += discount.Name + ", ";
}
//to remove the trailing comma and space
discountNames = discountNames.Substring(0, discountNames.Length - 2);

model.DiscountName = discountNames;


This is also assuming you already checked that AppliedDiscounts.Count > 0.
Il y a 12 ans
Fantastic, thanks !!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.