Custom Info provider example
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 OrderInfoProvider, which the E-commerce solution uses to manage product orders. The example adds a custom action when creating orders – for orders that fulfill certain conditions, credit is added to the given customer.
You can use the same general approach to customize any standard provider, helper or manager class in Xperience (i.e. those that inherit from the AbstractInfoProvider, AbstractHelper or AbstractManager class respectively).
Writing the custom provider
Start by preparing a separate project for custom classes in your Xperience solution:
- Open your Xperience solution in Visual Studio.
- Add a custom assembly (Class Library project) with class discovery enabled to the solution, or re-use an existing assembly.
- Reference the project from both your live site project and the Xperience administration project (CMSApp).
Continue by implementing the custom provider class:
Add a new class under the custom project. For example, name the class CustomOrderInfoProvider.cs.
Modify the class’s declaration so that it inherits from the CMS.Ecommerce.OrderInfoProvider class.
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.
Override the class’s SetOrderInfoInternal method.
Register the custom provider by adding the RegisterCustomProvider assembly attribute above the class declaration (requires a using statement for the CMS namespace). The attribute’s parameter specifies the type of the custom provider class as a System.Type object.
using System; using CMS; using CMS.Ecommerce; using CMS.SiteProvider; // Registers the custom OrderInfoProvider [assembly: RegisterCustomProvider(typeof(CustomOrderInfoProvider))] public class CustomOrderInfoProvider : OrderInfoProvider { /// <summary> /// Sets (updates or inserts) the specified order. /// </summary> public override void Set(OrderInfo info) { if (info == null) { throw new ArgumentNullException(nameof(info)); } // Indicates whether the set object is a new order bool newOrder = info.OrderID <= 0; // Updates the order or creates a new order using the default API of the base class base.Set(info); // Adds extra credit for each new order with a total price higher than 1000 if (newOrder && (info.OrderTotalPriceInMainCurrency > 1000)) { // Creates a new credit event CreditEventInfo extraCredit = new CreditEventInfo(); // Sets the credit event's general properties extraCredit.EventName = string.Format("Credit for your order: {0}", info.OrderID); extraCredit.EventDate = DateTime.Now; extraCredit.EventDescription = "Thank you for your order."; extraCredit.EventCustomerID = info.OrderCustomerID; // Sets the credit event's value in the site main currency extraCredit.EventCreditChange = 10; // Sets credit as site credit or as global credit according to the settings extraCredit.EventSiteID = ECommerceHelper.GetSiteID(SiteContext.CurrentSiteID, ECommerceSettings.USE_GLOBAL_CREDIT); // Saves the credit event CreditEventInfo.Provider.Set(extraCredit); } } }
Save all changes and Rebuild the solution.
The override of the Set method ensures that customers who purchase orders with a total price higher than 1000 (in the site’s main currency) receive extra customer credit. You can use the same approach to add any other types of custom actions.