Skip to content

Use the Microsoft Cognitive Services Recommendations API with EPiServer Commerce

License

Notifications You must be signed in to change notification settings

jstemerdink/EPi.Libraries.Recommendations

Repository files navigation

EPi.Libraries.Recommendations

By Jeroen Stemerdink

GitHub version Semver ![Platform](https://img.shields.io/badge/platform-.NET 4.5.2-blue.svg?style=flat) Platform Issue Count Stories in Backlog

About

Use the Microsoft Cognitive Services Recommendations API with EPiServer Commerce

Parts

Core

Export catalog

Export usage stats

Setings

Add an account key to your appsettings: <add key="recommendations:accountkey" value="YourKey" />

If you want to use the 'Frequently Bought Together build' change the key in the appSettings <add key="recommendations:useftbbuild" value="true" />

If the baseuri for the API changes, update the key in the appSettings <add key="recommendations:baseuri" value="https://westus.api.cognitive.microsoft.com/recommendations/v4.0" />

If you want to use a different model name, update the key in the appSettings <add key="recommendations:modelname" value="EPiServerCommerce" />

If you want to use a different display name for the catalog, update the key in the appSettings <add key="recommendations:catalogdisplayname" value="EPiServer Commerce catalog" />

If you want to use a different display name for the usages, update the key in the appSettings <add key="recommendations:usagedisplayname" value="EPiServer Commerce catalog usages" />

Implementation

As all commerce setups differ you will need to implement your own version of the recommendation service. Probably you will only need to override the GetCatalogItems. Below you find an example for QuickSilver.

    [ServiceConfiguration(typeof(IRecommendationService), Lifecycle = ServiceInstanceScope.Singleton)]
    public class QuicksilverRecommendationService : RecommendationService
    {
        public QuicksilverRecommendationService(RecommendationsApiWrapper recommendationsApiWrapper, RecommendationSettingsRepository recommendationSettingsRepository, IContentLoader contentLoader, ReferenceConverter referenceConverter, IOrderRepository orderRepository, ILogger log )
            : base(recommendationsApiWrapper, recommendationSettingsRepository, contentLoader, referenceConverter, orderRepository, log)
        {
        }

        public override List<UsageItem> GetUsageItems(DateTime since)
        {
            List<PurchaseOrder> orders = OrderContext.Current.FindPurchaseOrdersByStatus(OrderStatus.InProgress, OrderStatus.Completed, OrderStatus.OnHold, OrderStatus.AwaitingExchange, OrderStatus.PartiallyShipped).Where(po => po.Created > since).ToList();
            
            IEnumerable<UsageItem> usageItems =
                from purchaseOrder in orders
                from lineItem in purchaseOrder.GetAllLineItems()
                select new UsageItem { UserID = purchaseOrder.CustomerId.ToString(), ItemID = lineItem.Code, EventDate = purchaseOrder.Created, EventType = EventType.Purchase };

            return usageItems.ToList();
        }

        public override List<CatalogItem> GetCatalogItems(DateTime since)
        {
            List<CatalogItem> catalogItems = new List<CatalogItem>();

            IEnumerable<ContentReference> descendents =
                this.ContentLoader.GetDescendents(this.ReferenceConverter.GetRootLink());

            foreach (ContentReference contentReference in descendents)
            {
                FashionVariant variation;

                if (!this.ContentLoader.TryGet(contentReference, out variation))
                {
                    continue;
                }

                if (variation.Created < since)
                {
                    continue;
                }

                NodeContent node =
                    this.ContentLoader.GetAncestors(contentReference).OfType<NodeContent>().FirstOrDefault();

                CatalogItem catalogItem = new CatalogItem
                                              {
                                                  ItemID = variation.Code,
                                                  ItemName = variation.Name,
                                                  ProductCategory = node == null ? "undefined" : node.Name,
                                                  Description = string.Empty,
                                                  Features = new Dictionary<string, string>() { { "color", variation.Color }, { "size", variation.Size } }
                                              };

                catalogItems.Add(catalogItem);
            }

            return catalogItems;
        }

        public override void SendUsageEvent(int quantity, string code, decimal unitPrice, EventType eventType)
        {
            UsageEvent usageEvent = Helpers.CreateUsageEventContent(quantity, code, unitPrice, eventType);
            this.SendUsageEvent(usageEvent);
        }
    }
}

You can override some more methods if you need to, this is the full list:

        List<CatalogItem> GetCatalogItems(DateTime since);
        List<EntryContentBase> GetItemRecommendations(string itemIds);
        List<T> GetItemRecommendations<T>(string itemIds);
        List<EntryContentBase> GetItemRecommendations(string itemIds, int numberOfResults);
        List<T> GetItemRecommendations<T>(string itemIds, int numberOfResults);
        List<UsageItem> GetUsageItems(DateTime since);
        List<EntryContentBase> GetUserRecommendations();
        List<T> GetUserRecommendations<T>();
        List<EntryContentBase> GetUserRecommendations(int numberOfResults);
        List<T> GetUserRecommendations<T>(int numberOfResults);
        void SendUsageEvent(int quantity, string code, decimal unitPrice, EventType eventType);
        void SendUsageEvent(decimal quantity, string code, decimal unitPrice, EventType eventType);

Powered by ReSharper

image

About

Use the Microsoft Cognitive Services Recommendations API with EPiServer Commerce

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published