Displaying discount values
When using discounts or free shipping offers on your e-commerce site, you may want to display the discounted prices or display banners that show how much more customers need to spend to obtain free shipping. This page describes how to use API provided by theKentico.Xperience.Libraries integration package to display such information.
Displaying catalog discounts
In a controller’s action, use the GetPricesmethod from an implementation of ICatalogPriceCalculator in the CMS.Ecommerce namespace (installed into your project as part of the Kentico.Xperience.Libraries integration package). The method’s calculations, in its default implementation, automatically include any catalog discounts that apply for the current customer.
The GetPrices method returns a ProductCatalogPrices object. You can use the object’s StandardPrice and Price properties to find the amount reduced by catalog discounts via subtraction.
// Gets the current shopping cart using IShoppingService
ShoppingCartInfo shoppingCart = shoppingService.GetCurrentShoppingCart();
// Calculates prices for the specified product using ICatalogPriceCalculatorFactory
ProductCatalogPrices price = catalogPriceCalculatorFactory
.GetCalculator(shoppingCart.ShoppingCartSiteID)
.GetPrices(sku, Enumerable.Empty<SKUInfo>(), shoppingCart);
// Gets the catalog discount
decimal catalogDiscount = price.StandardPrice - price.Price;
We recommend using a dependency injection container to initialize service and factory instances (e.g. the IShoppingService and ICatalogPriceCalculatorFactory instances used in the example above).
You can then use the discount value in your views.
Displaying free shipping offers
In a controller’s action, call the CalculateRemainingAmountForFreeShipping method of the shopping cart object.
The method returns the remaining value of products the customer needs to add to the shopping cart to be eligible for free shipping. If the cart is null, if there is no valid discount, or if free shipping was already applied, the method returns 0.
// Gets the current shopping cart
ShoppingCartInfo shoppingCart = shoppingService.GetCurrentShoppingCart();
// Gets the remaining amount for free shipping
decimal remainingFreeShipping = shoppingCart.CalculateRemainingAmountForFreeShipping();
We recommend using a dependency injection container to initialize service instances (e.g. the IShoppingService instance used in the example above).
You can then display or use the amount remaining in your views.