Customizing product prices

In Kentico, you can modify the way of retrieving a product’s price. On this page, you can also see examples of product price customization.

To customize retrieving prices, you need to implement a class that inherits from the SKUInfoProvider class:

  1. Create a new class in your project in Visual Studio (for example CustomSKUInfoProvider.cs) that inherits from the SKUInfoProvider class and registers the custom InfoProvider:

    
    
    
     [assembly: RegisterCustomProvider(typeof(CustomSKUInfoProvider))]
     public class CustomSKUInfoProvider : SKUInfoProvider
     {
     }
    
    
     
  2. Implement a method that overrides a default SKUInfoProvider method.

  3. Save your project and reload you Kentico website.

If you open your website, Kentico uses the modified SKUInfoProvider class with the modified methods.

RecalculateProductsInternal

Recalculates products’ and product options’ prices (e.g., product prices, list prices, and donation’s minimum and maximum donation amount) using a specified coefficient (float coefficient) to a specified number of digits (int roundTo) on a specified site (int siteId). You can filter products with an additional WHERE SQL condition (string where).




protected virtual void RecalculateProductsInternal(float coefficient, int roundTo, string where, int siteId) { }


GetSKUPriceInternal

Returns a price of a specified product or product option (SKUInfo sku) based on the product’s and the shopping cart’s data (ShoppingCartInfo cart).

You can set whether discounts and taxes are calculated (bool discounts and bool taxes). You can also set whether the system calls the method within a shopping cart calculation, or within displaying the price on the live site (bool forCart). You can set which database column the system uses for the price retrieving (string column); otherwise, the SKUPrice column is used.

If the SKU is a product option’s SKU, you can specify the parent SKU (SKUInfo parentSku) for tax and discount calculation. You can also specify a custom price (double customPrice) to calculate with a different then the SKU’s price.




protected virtual double GetSKUPriceInternal(SKUInfo sku, ShoppingCartInfo cart) { }
--OR--
protected virtual double GetSKUPriceInternal(SKUInfo sku, ShoppingCartInfo cart, string column) { }
--OR--
protected virtual double GetSKUPriceInternal(SKUInfo sku, ShoppingCartInfo cart, bool discounts, bool taxes, bool forCart, string column, SKUInfo parentSku = null, double customPrice = Double.NaN) { } 


See examples below

GetSKUFormattedPriceInternal

Returns a formatted price of a specified product or product option (SKUInfo sku) based on the product’s and the shopping cart’s (ShoppingCartInfo cart) data. Set whether discounts and taxes are calculated (bool discounts and bool taxes).

You can set which database column the system uses for the price retrieving (string column); otherwise, the SKUPrice column is used.




protected virtual string GetSKUFormattedPriceInternal(SKUInfo sku, ShoppingCartInfo cart, bool discounts, bool taxes, string column) { }


CalculateSKUPriceAfterDiscountInternal

Returns a price of a specified product or product option (SKUInfo sku) after all its catalog discounts are applied based on the product’s and the shopping cart’s ShoppingCartInfo cart) data. Set whether the system calls the method within a shopping cart calculation, or within displaying the price on the live site (bool forCart).

You can set which database column the system uses for the price retrieving (string column); otherwise, the SKUPrice column is used.

If the SKU is a product option’s SKU, you can specify the parent SKU (SKUInfo parentSku) for tax and discount calculation. You can also specify a custom price (double customPrice) to calculate with a different then the SKU’s price.




protected virtual double CalculateSKUPriceAfterDiscountInternal(SKUInfo sku, ShoppingCartInfo cart, bool forCart, string column, SKUInfo parentSku = null, double customPrice = Double.NaN) { }


CalculateSKUDiscountInternal

Returns an amount of a specified catalog discount (IItemDiscount discount) that is applied to a specified product (SKUInfo sku) in a specified shopping cart (ShoppingCartInfo cart). By default,  theCalculateSKUPriceAfterDiscountInternalmethod calls this method for every catalog discount and therefore, you need to specify the product’s price after applying previous discounts (double priceAfterDiscount). If there are not any previously applied catalog discounts, you can use the SKU’s price.

The method returns the noted price if the amount of the discount is bigger than the price.




protected virtual double CalculateSKUDiscountInternal(SKUInfo sku, ShoppingCartInfo cart, IItemDiscount discount, double priceAfterDiscount) { }


CalculateSKUTotalTaxInternal

Returns the total tax amount of a specified product or product option (SKUInfo sku) in a specified shopping cart (ShoppingCartInfo cart). Specify the price after applying discounts to which taxes apply (double priceAfterDiscount).

If the SKU is a product option’s SKU, specify the option’s parent SKU for tax calculation purposes (SKUInfo parentSku).

The result is cached according to the cache settings (opposed to theCalculateSKUTotalTaxFromDBInternalmethod).




protected virtual double CalculateSKUTotalTaxInternal(SKUInfo sku, ShoppingCartInfo cart, double priceAfterDiscount, SKUInfo parentSku = null) { }


CalculateSKUTotalTaxFromDBInternal

Returns the total tax amount of a specified product or product option (SKUInfo sku) in a specified shopping cart (ShoppingCartInfo cart). Specify the price after applying discounts to which taxes apply (double priceAfterDiscount).

If the SKU is a product option’s SKU, specify the option’s parent SKU for tax calculation purposes (SKUInfo parentSku).

The result is not cached (opposed to theCalculateSKUTotalTaxInternalmethod).




protected virtual double CalculateSKUTotalTaxFromDBInternal(SKUInfo sku, ShoppingCartInfo cart, double priceAfterDiscount, SKUInfo parentSku = null) { } 


Examples

Getting the product prices based on the visitor’s culture

This example demonstrates how to customize the GetSKUPriceInternal method to return different prices for different visitors’ cultures. The culture is stored among the shopping cart data.

The example uses custom database columns (SKUEnUsPrice and SKUEnUkPrice). To add a custom database column:

  1. Open the Modules application.
  2. Edit () the E-commerce module.
  3. Switch to the Classes tab.
  4. Edit () the SKU class.
  5. Switch to the Fields tab.
  6. Create a new field based on your needs.
  7. Click Save.

The new database column is ready to use. Repeat the steps to create more columns.




using CMS;
using CMS.Ecommerce;
using CMS.Helpers;
using CMS.Base;

[assembly: RegisterCustomProvider(typeof(CustomSKUInfoProvider))]
public class CustomSKUInfoProvider : SKUInfoProvider
{
    protected override double GetSKUPriceInternal(SKUInfo sku, ShoppingCartInfo cart)
    {
        double price = 0;

        // Checks whether the shopping cart data exists.
        if (cart != null)
        {
            // Selects the shopping cart's culture.
            switch (cart.ShoppingCartCulture.ToLowerCSafe())
            {
                case "en-us":
                    // Gets the product's price from the SKUEnUsPrice database column.
                    price = ValidationHelper.GetDouble(sku.GetValue("SKUEnUsPrice"), 0);
                    break;
                case "en-uk":
                    // Gets the product's price from the SKUEnUkPrice database column.
                    price = ValidationHelper.GetDouble(sku.GetValue("SKUEnUkPrice"), 0);
                    break;
            }
        }

        // Checks whether a custom price was assigned.
        if (price == 0)
        {
            // Gets the price from the default SKUPrice database column.
            return base.GetSKUPriceInternal(sku, cart);
        }

        // Returns the custom price.
        return price;
    }
}


Getting the product prices based on the product’s public status

This example demonstrates how to customize the GetSKUPriceInternal method to return different prices of products that have a specific public product status.

The example uses a custom database column (SKUDiscountedPrice). See the previous example to see how to add new database columns.




using CMS;
using CMS.Ecommerce;
using CMS.Helpers;
using CMS.Base;

[assembly: RegisterCustomProvider(typeof(CustomSKUInfoProvider))]
public class CustomSKUInfoProvider : SKUInfoProvider
{
    protected override double GetSKUPriceInternal(SKUInfo sku, ShoppingCartInfo cart)
    {
        double price = 0;

        // Gets the product's public product status.
        PublicStatusInfo status = PublicStatusInfoProvider.GetPublicStatusInfo(sku.SKUPublicStatusID);

        // Checks whether the product has the 'discounted' status.
        if ((status != null) && (status.PublicStatusName.ToLowerCSafe() == "discounted"))
        {
            // Gets the product's price from a special database column - SKUDiscountedPrice.
            price = ValidationHelper.GetDouble(sku.GetValue("SKUDiscountedPrice"), 0);
        }

        // Checks whether a custom price was assigned.
        if (price == 0)
        {
            // Gets the price from the default SKUPrice database column.
            return base.GetSKUPriceInternal(sku, cart);
        }

        // Returns the custom price.
        return price;
    }
}