Kentico Custom Pricing

shopping.jpg

Recently I undertook a project with a client alongside an agency to deliver their new E-Commerce website. When we looked in the project requirements it was obvious that the standard product pricing within Kentico was not going to work for their requirements and that some bespoke custom logic was required.

The client had a product pricing system based off a grid for the width and then a quantity of how many at that size you wanted. So for example you could purchase the same product in 2m squared, 4m squared, 6m squared etc. So a product might be £5 for 2 metre squared, £10 for 4 metre squared but then £12 for 6m squared.

Firstly all of the products in the system were entered with £0 as the standard price in Kentico.

I added a custom table into Kentico which stored the pricing grids and then associated these items to the products. This gave the client the ability to add pricing grids into Kentico where they would pick a width and then a cost for that width. Each pricing grid could be associated to multiple products though each product could only be associated to one pricing grid.

I then wrote some custom logic to generate the price for the product based on the price grid associated to the current document and the width/quantity entered by the user and displayed this amount to the user. This was obviously quite straight forward.

However when I added the product to the shopping cart, because the standard Kentico price was £0 then this is what was displayed to the user. What I now needed to do was to alter Kentico's internal logic for the shopping cart item to look at my custom price grid rather than looking at the standard price within Kentico. I achieved this by using a custom info provider for the shopping cart item info and by overriding the CalculateUnitPriceInternal method. You can find out more about this approach in the Kentico documentation here https://docs.kentico.com/k10/custom-development/customizing-providers/custom-info-provider-example.

Before I could override the method I needed to ensure that my custom property (width) was accessible within the overridden method. To do this I created a custom field against the ShoppingCartItem class within the Kentico interface. This then allowed me to pass in the width that the user had entered when adding the item to the shopping basket.

To override the CalculateUnitPriceInternal method within my custom info provider I simply needed to call the method I created earlier for displaying the price passing in the SKU object from the ShoppingCartItemInfo along with the width field from the ShoppingCartItemInfo object that I added earlier in the process. This then returned the price to Kentico (exc VAT) and told it to use this price instead of the standard price (£0) within Kentico. The rest of the logic for tax/total shopping cart price etc. always uses this new logic when determining the prices.

Lovely and simple and really easy to extend Kentico's E-Commerce pricing to use a custom pricing model instead :)

Back to all blog posts

Matt Swain

Freelance .NET Developer