Ejemplo n.º 1
0
        private IRecommendations GetRecommendedProducts(RecommendedProductsBlock currentBlock)
        {
            IRecommendations recommendedProducts;
            CultureInfo      currentCulture = ContentLanguage.PreferredCulture;
            int maxCount = 6;

            if (currentBlock.MaxCount > 0)
            {
                maxCount = currentBlock.MaxCount;
            }
            if (currentBlock.Category != null)
            {
                NodeContent catalogNode = _contentLoader.Get <NodeContent>(currentBlock.Category);
                var         code        = catalogNode.Code;
                recommendedProducts =
                    _recommendationService.GetRecommendedProductsByCategory(_currentCustomerService.GetCurrentUserId(),
                                                                            new List <string> {
                    code
                },
                                                                            maxCount,
                                                                            currentCulture);
            }
            else
            {
                recommendedProducts =
                    _recommendationService.GetRecommendedProducts(_currentCustomerService.GetCurrentUserId(), maxCount,
                                                                  currentCulture);
            }
            return(recommendedProducts);
        }
Ejemplo n.º 2
0
 protected void PopulateRecommendations(CartModel model, int maxCount = 6)
 {
     if (model.LineItems.Any())
     {
         var recommendedProductsForCart =
             _recommendationService.GetRecommendedProductsForCart(_currentCustomerService.GetCurrentUserId(),
                                                                  model.LineItems.Select(x => x.Code).ToList(),
                                                                  maxCount,
                                                                  _currentMarket.GetCurrentMarket().DefaultLanguage
                                                                  );
         List <ProductListViewModel> recommendedProductList = new List <ProductListViewModel>();
         if (recommendedProductsForCart != null && recommendedProductsForCart.Products != null)
         {
             foreach (var product in recommendedProductsForCart.Products)
             {
                 IProductListViewModelInitializer modelInitializer = product as IProductListViewModelInitializer;
                 if (modelInitializer != null)
                 {
                     var viewModel = _productService.GetProductListViewModel(modelInitializer);
                     recommendedProductList.Add(viewModel);
                     // Track
                     HttpContext.AddRecommendationExposure(new TrackedRecommendation()
                     {
                         ProductCode = viewModel.Code, RecommenderName = recommendedProductsForCart.RecommenderName
                     });
                 }
             }
             model.Recommendations             = recommendedProductList;
             model.RecommendationsTrackingName = recommendedProductsForCart.RecommenderName;
         }
     }
 }
        private List <FindProduct> GetRecommendedProducts(ProductSearchData productSearchData, string language)
        {
            List <FindProduct> recommendedFindProducts = new List <FindProduct>();
            CultureInfo        currentCulture          = new CultureInfo(language);
            List <string>      categoryCodes           = new List <string>();

            foreach (var categoryId in productSearchData.ProductData.SelectedProductCategories)
            {
                ContentReference catalogNodeRef = _referenceConverter.GetContentLink(categoryId, CatalogContentType.CatalogNode, 0);
                var catalogNode = _contentLoader.Get <NodeContent>(catalogNodeRef);
                if (catalogNode != null)
                {
                    categoryCodes.Add(catalogNode.Code);
                }
            }

            var recommendedProducts =
                _recommendationService.GetRecommendedProductsByCategory(_currentCustomerService.GetCurrentUserId(),
                                                                        categoryCodes,
                                                                        3,
                                                                        currentCulture);

            if (recommendedProducts != null && recommendedProducts.Products != null && recommendedProducts.Products.Any())
            {
                var currentMarket = ServiceLocator.Current.GetInstance <Mediachase.Commerce.ICurrentMarket>().GetCurrentMarket();

                foreach (var product in recommendedProducts.Products)
                {
                    if (product is IIndexableContent)
                    {
                        var productContent = product as IIndexableContent;
                        var findProduct    = productContent.GetFindProduct(currentMarket);
                        recommendedFindProducts.Add(findProduct);
                    }
                }
            }
            return(recommendedFindProducts);
        }
Ejemplo n.º 4
0
        public IEnumerable <ProductListViewModel> GetSimilarProducts(int contentId)
        {
            const int maxRecommendedProducts   = 10;
            List <ProductListViewModel> models = new List <ProductListViewModel>();

            SetLanguage();
            string           language    = Language;
            ContentReference contentLink = _referenceConverter.GetContentLink(contentId, CatalogContentType.CatalogEntry, 0);
            IContent         contentItem;

            if (_contentLoader.TryGet(contentLink, out contentItem))
            {
                EntryContentBase entryContent = contentItem as EntryContentBase;
                if (entryContent != null)
                {
                    var currentCustomer     = CustomerContext.Current.CurrentContact;
                    var recommendedProducts = _recommendationService.GetRecommendedProducts(entryContent,
                                                                                            _currentCustomerService.GetCurrentUserId(), maxRecommendedProducts);


                    foreach (var content in recommendedProducts.Products)
                    {
                        ProductListViewModel model     = null;
                        VariationContent     variation = content as VariationContent;
                        if (variation != null)
                        {
                            model = new ProductListViewModel(variation, _currentMarket.GetCurrentMarket(),
                                                             currentCustomer);
                        }
                        else
                        {
                            ProductContent product = content as ProductContent;
                            if (product != null)
                            {
                                model = _productService.GetProductListViewModel(product as IProductListViewModelInitializer);

                                // Fallback
                                if (model == null)
                                {
                                    model = new ProductListViewModel(product, _currentMarket.GetCurrentMarket(),
                                                                     currentCustomer);
                                }
                            }
                        }

                        if (model != null)
                        {
                            models.Add(model);
                        }
                    }
                }
            }

            return(models);
        }