|
||
The system uses Info providers to work with individual types of objects. A provider class contains methods for creating, updating, getting, and deleting the data of the corresponding objects. By customizing the appropriate Info provider, you can change or extend the functionality of nearly any object in the system.
The following example customizes the ShippingOptionInfoProvider, which the E-commerce module uses to manage shipping options for product orders. The sample customization modifies how the provider calculates shipping costs.
You can use the same general approach to customize any standard provider or helper class in Kentico CMS (i.e. those that inherit from the AbstractProvider or AbstractHelper class respectively).
1. Open your web project in Visual Studio and create a new class in the App_Code folder (or Old_App_Code if the project is installed as a web application). For example, name the class CustomShippingOptionInfoProvider.cs.
2. Edit the class and add the following references:
[C#]
using CMS.Ecommerce; |
3. Modify the class's declaration so that it inherits from the CMS.Ecommerce.ShippingOptionInfoProvider class:
[C#]
/// <summary> |
Custom providers must always inherit from the original provider class. This ensures that the custom provider supports all of the default functionality and allows you to add your own customizations by overriding the existing members of the class.
4. Add the following override of the CalculateShippingInternal method into the class:
[C#]
/// <summary> |
This override of the CalculateShippingInternal method ensures that:
•Users who belong to the Gold partner role always have free shipping
•An extra flat charge is added for customers with shipping addresses outside of the USA
Note: The override gets the standard shipping price by calling the base method from the parent class. This allows you to calculate the default price based on the shipping option settings, and then modify it as needed.
Use the following steps to register your custom provider directly in App_Code:
1. Extend the CMSModuleLoader partial class. You can either create a new App_Code class file for this purpose or add the partial class declaration directly into the file containing your custom provider (CustomShippingOptionInfoProvider.cs in this case).
[C#]
public partial class CMSModuleLoader |
2. Create a new attribute class inside the CMSModuleLoader that inherits from CMSLoaderAttribute. Add the attribute to the CMSModuleLoader partial class:
[C#]
using CMS.SettingsProvider; |
3. Override the Init method inside the attribute class and assign a new instance of the CustomShippingOptionInfoProvider class into the ProviderObject property of the original ShippingOptionInfoProvider.
[C#]
using CMS.Ecommerce; |
You can try out how the customization affects the e-commerce module on the sample Corporate site.
1. Open the live website and log on as the Sample Gold Partner user (user name gold).
2. Navigate to the Products section and add any product to the shopping cart.
3. Go through the checkout process for the order.
In the Order preview (step 5 of the checkout process), you can see that the shipping is always free, no matter what shipping address you entered.
If you repeat the same process as a regular user (e.g. Andy), the system calculates the shipping costs normally and adds the extra charge if the shipping address is in a different country than the USA.