roundingkenticocommercekentico-11

How to propagate the rounding configuration in a Kentico store settings for custom calculations?


I am developing a commerce site in Kentico 11 CMS and, in addition to a product price, need to compute its insurance cost based on a user’s input.

Kentico allows to configure a price rounding option in Store configuration application -> Store settings -> General tab and I would like to use it for my custom calculations too, so that my bespoke functionality rounds the result according to the number of decimal places as per the global store setting.

As per Kentico documentation you can customise the global rounding by creation of a Service class implementing IRoundingService interface with the Round() method according to your specific requirements and a SeviceFactory class implementing IRoundingServiceFactory interface with the GetRoundingService() method. Ideally, I would like to avoid this as I am happy with the standard financial rounding option in Kentico and simply want to apply it to my custom functionality for consistency.


Solution

  • You can retrieve the rounding service for the context site as follows:

    using CMS.Core;
    using CMS.Ecommerce;
    using CMS.SiteProvider;
    IRoundingService roundingService = Service.Resolve<IRoundingServiceFactory().GetRoundingService(SiteContext.CurrentSiteID);
    

    Then call the rounding method as roundingService.Round(insurance, currency) passing to it a decimal value and the current currency.

    The current currency you can get as below:

    CurrencyInfo currencyInfo = ECommerceContext.CurrentCurrency;