예제 #1
0
        protected override object DoExecute(Commerce.CMSIntegration.DataSources.CommerceDataSourceContext context, ParsedGenericCommerceDataSourceSettings settings)
        {
            var userId = context.HttpContext.GetVisitorUniqueId();

            var           top = settings.Top.GetValueOrDefault(4);
            ISet <string> toIgnoreItems;

            var engine = GetRecommendationEngine(context, settings, out toIgnoreItems);

            var items   = engine.Recommend(userId, top, toIgnoreItems);
            var itemIds = items.Select(it => Convert.ToInt32(it.ItemId)).ToArray();

            var reasonItemIds = new List <int>();

            foreach (var item in items)
            {
                var reasonId = item.GetBestReasonItemId();
                if (reasonId > 0)
                {
                    reasonItemIds.Add(reasonId);
                }
            }

            var products = context.Site.Commerce()
                           .Products.Query().ByIds(itemIds)
                           .Include(p => p.Brand)
                           .Include(p => p.Images)
                           .Include(p => p.Variants)
                           .ToList();

            var reasons = context.Site.Commerce()
                          .Products.Query().ByIds(reasonItemIds.ToArray())
                          .ToList();

            var result = new List <ProductRecommendation>();

            foreach (var item in items)
            {
                var productId = Convert.ToInt32(item.ItemId);
                var product   = products.Find(p => p.Id == productId);
                if (product != null)
                {
                    var recommendation = new ProductRecommendation
                    {
                        Product = product,
                        Weight  = item.Weight
                    };

                    var reasonId = item.GetBestReasonItemId();
                    if (reasonId > 0)
                    {
                        recommendation.Reason = reasons.Find(p => p.Id == reasonId);
                    }

                    result.Add(recommendation);
                }
            }

            return(result);
        }
예제 #2
0
        protected override object DoExecute(Commerce.CMSIntegration.DataSources.CommerceDataSourceContext context, ParsedGenericCommerceDataSourceSettings settings)
        {
            var userId = context.HttpContext.GetVisitorUniqueId();

            var top = settings.Top.GetValueOrDefault(4);
            ISet<string> toIgnoreItems;

            var engine = GetRecommendationEngine(context, settings, out toIgnoreItems);

            var items = engine.Recommend(userId, top, toIgnoreItems);
            var itemIds = items.Select(it => Convert.ToInt32(it.ItemId)).ToArray();

            var reasonItemIds = new List<int>();
            foreach (var item in items)
            {
                var reasonId = item.GetBestReasonItemId();
                if (reasonId > 0)
                {
                    reasonItemIds.Add(reasonId);
                }
            }

            var products = context.Site.Commerce()
                                  .Products.Query().ByIds(itemIds)
                                  .Include(p => p.Brand)
                                  .Include(p => p.Images)
                                  .Include(p => p.Variants)
                                  .ToList();

            var reasons = context.Site.Commerce()
                                 .Products.Query().ByIds(reasonItemIds.ToArray())
                                 .ToList();

            var result = new List<ProductRecommendation>();

            foreach (var item in items)
            {
                var productId = Convert.ToInt32(item.ItemId);
                var product = products.Find(p => p.Id == productId);
                if (product != null)
                {
                    var recommendation = new ProductRecommendation
                    {
                        Product = product,
                        Weight = item.Weight
                    };

                    var reasonId = item.GetBestReasonItemId();
                    if (reasonId > 0)
                    {
                        recommendation.Reason = reasons.Find(p => p.Id == reasonId);
                    }

                    result.Add(recommendation);
                }
            }

            return result;
        }