/// <summary>
        /// This method returns child products for this category
        /// </summary>
        /// <param name="searchOptions">The options to perform the search with</param>
        /// <param name="searchKeyword">The keyword to search for</param>
        /// <param name="catalogName">The name of the catalog to search against</param>
        /// <returns>A list of child products</returns>
        protected SearchResults GetChildProducts(CommerceSearchOptions searchOptions, string searchKeyword, string catalogName)
        {
            if (this.CurrentSiteContext.Items[CurrentSearchProductResultsKeyName] != null)
            {
                return((SearchResults)this.CurrentSiteContext.Items[CurrentSearchProductResultsKeyName]);
            }

            Assert.ArgumentNotNull(searchKeyword, "searchOptions");
            Assert.ArgumentNotNull(searchKeyword, "searchKeyword");
            Assert.ArgumentNotNull(searchKeyword, "catalogName");

            var returnList        = new List <Item>();
            var totalPageCount    = 0;
            var totalProductCount = 0;
            IEnumerable <CommerceQueryFacet> facets = Enumerable.Empty <CommerceQueryFacet>();

            if (Item != null && !string.IsNullOrEmpty(searchKeyword.Trim()))
            {
                SearchResponse searchResponse = null;
                searchResponse = SearchNavigation.SearchCatalogItemsByKeyword(searchKeyword, catalogName, searchOptions);

                if (searchResponse != null)
                {
                    returnList.AddRange(searchResponse.ResponseItems);
                    totalProductCount = searchResponse.TotalItemCount;
                    totalPageCount    = searchResponse.TotalPageCount;
                    facets            = searchResponse.Facets;
                }
            }

            var results = new SearchResults(returnList, totalProductCount, totalPageCount, searchOptions.StartPageIndex, facets);

            this.CurrentSiteContext.Items[CurrentSearchProductResultsKeyName] = results;
            return(results);
        }
Example #2
0
        public ProductViewModel GetWildCardProductViewModel(Item datasource, Rendering currentRendering)
        {
            ProductViewModel productViewModel;
            var productId = CatalogUrlManager.ExtractItemIdFromCurrentUrl().Replace(" ", "-");
            var virtualProductCacheKey = string.Format(CultureInfo.InvariantCulture, "VirtualProduct_{0}", productId);

            if (this.CurrentSiteContext.Items.Contains(virtualProductCacheKey))
            {
                productViewModel = this.CurrentSiteContext.Items[virtualProductCacheKey] as ProductViewModel;
            }
            else
            {
                if (string.IsNullOrEmpty(productId))
                {
                    //No ProductId passed in on the URL
                    //Use to Storefront DefaultProductId
                    productId = StorefrontManager.CurrentStorefront.DefaultProductId;
                }

                var productItem = SearchNavigation.GetProduct(productId, this.CurrentCatalog.Name);

                if (productItem == null)
                {
                    var message = string.Format(CultureInfo.InvariantCulture, "The requested product '{0}' does not exist in the catalog '{1}' or cannot be displayed in the language '{2}'", productId, this.CurrentCatalog.Name, Context.Language);
                    Log.Error(message, this);
                    throw new InvalidOperationException(message);
                }

                datasource       = productItem;
                productViewModel = this.GetProductViewModel(datasource, currentRendering);
                this.CurrentSiteContext.Items.Add(virtualProductCacheKey, productViewModel);
            }

            return(productViewModel);
        }
Example #3
0
        public ActionResult GiftCardRating()
        {
            this.Item = SearchNavigation.GetProduct(StorefrontManager.CurrentStorefront.GiftCardProductId, CurrentCatalog.Name);
            var productViewModel = _giftCardRepository.GetGiftCardViewModel(this.Item, this.CurrentRendering);

            return(View("GiftCardRating", productViewModel));
        }
        public JsonResult GetCurrentProductStockInfo(ProductStockInfoInputModel model)
        {
            try
            {
                Assert.ArgumentNotNull(model, "model");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var currentProductItem = SearchNavigation.GetProduct(model.ProductId, CurrentCatalog.Name);
                var productId          = currentProductItem.Name;
                var catalogName        = currentProductItem["CatalogName"];
                var products           = new List <CommerceInventoryProduct>();
                if (currentProductItem.HasChildren)
                {
                    foreach (Item item in currentProductItem.Children)
                    {
                        products.Add(new CommerceInventoryProduct
                        {
                            ProductId   = productId,
                            CatalogName = catalogName,
                            VariantId   = item["VariantId"]
                        });
                    }
                }
                else
                {
                    products.Add(new CommerceInventoryProduct {
                        ProductId = productId, CatalogName = catalogName
                    });
                }

                var response = this.InventoryManager.GetStockInformation(this.CurrentStorefront, products, StockDetailsLevel.All);
                var result   = new StockInfoListBaseJsonResult(response.ServiceProviderResult);
                if (response.Result == null)
                {
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }

                result.Initialize(response.Result);
                var stockInfo = response.Result.FirstOrDefault();
                if (stockInfo != null)
                {
                    this.InventoryManager.VisitedProductStockStatus(this.CurrentStorefront, stockInfo, string.Empty);
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("GetCurrentProductStockInfo", this, e);
                return(Json(new BaseJsonResult("GetCurrentProductStockInfo", e), JsonRequestBehavior.AllowGet));
            }
        }
 /// <summary>
 /// Gets the products stock status where lists of products are displayed on the site.
 /// </summary>
 /// <param name="storefront">The storefront.</param>
 /// <param name="productViewModels">The product view models.</param>
 public virtual void GetProductsStockStatusForList([NotNull] CommerceStorefront storefront, List <ProductViewModel> productViewModels)
 {
     if (!StorefrontManager.CurrentStorefront.UseIndexFileForProductStatusInLists)
     {
         this.GetProductsStockStatus(storefront, productViewModels);
     }
     else
     {
         SearchNavigation.GetProductStockStatusFromIndex(productViewModels);
     }
 }
Example #6
0
        /// <summary>
        /// Invoked when an item is clicked.
        /// </summary>
        /// <param name="sender">The GridView displaying the item clicked.</param>
        /// <param name="e">Event data that describes the item clicked.</param>
        void ItemView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var result = (SearchResult)e.ClickedItem;
            var nav    = new SearchNavigation()
            {
                Key  = result.Key,
                Type = result.Type,
            };

            //this.Frame.Navigate(typeof(ItemDetailPage), nav);
        }
        /// <summary>
        /// This method returns child categories for this category
        /// </summary>
        /// <param name="searchOptions">The options to perform the search with</param>
        /// <param name="categoryItem">The category item whose children to retrieve</param>
        /// <returns>A list of child categories</returns>
        protected CategorySearchResults GetChildCategories(CommerceSearchOptions searchOptions, Item categoryItem)
        {
            var returnList         = new List <Item>();
            var totalPageCount     = 0;
            var totalCategoryCount = 0;

            if (Item != null)
            {
                return(SearchNavigation.GetCategoryChildCategories(categoryItem.ID, searchOptions));
            }

            return(new CategorySearchResults(returnList, totalCategoryCount, totalPageCount, searchOptions.StartPageIndex, new List <FacetCategory>()));
        }
        public override ActionResult Index()
        {
            var dataSourceQuery = RenderingContext.Current.Rendering.DataSource;

            var navigationCategories = new List <Category>();

            if (!string.IsNullOrWhiteSpace(dataSourceQuery))
            {
                var response          = SearchNavigation.GetNavigationCategories(dataSourceQuery, new CommerceSearchOptions());
                var navigationResults = response.ResponseItems;
                navigationCategories.AddRange(navigationResults.Select(result => new Category(result)));
            }

            return(View(navigationCategories));
        }
        /// <summary>
        /// This method returns the ProductSearchResults for a datasource
        /// </summary>
        /// <param name="dataSource">The datasource to perform the search with</param>
        /// <param name="productSearchOptions">The search options.</param>
        /// <returns>A ProductSearchResults</returns>
        public SearchResults GetProductSearchResults(Item dataSource, CommerceSearchOptions productSearchOptions)
        {
            Assert.ArgumentNotNull(productSearchOptions, "productSearchOptions");

            if (dataSource != null)
            {
                int    totalProductCount = 0;
                int    totalPageCount    = 0;
                string error             = string.Empty;

                if (dataSource.TemplateName == StorefrontConstants.KnownTemplateNames.CommerceNamedSearch || dataSource.TemplateName == StorefrontConstants.KnownTemplateNames.NamedSearch)
                {
                    var returnList = new List <Item>();
                    IEnumerable <CommerceQueryFacet> facets = null;
                    var searchOptions          = new CommerceSearchOptions(-1, 0);
                    var defaultBucketQuery     = dataSource[CommerceConstants.KnownSitecoreFieldNames.DefaultBucketQuery];
                    var persistendBucketFilter = CleanLanguageFromFilter(dataSource[CommerceConstants.KnownSitecoreFieldNames.PersistentBucketFilter]);

                    try
                    {
                        var searchResponse = SearchNavigation.SearchCatalogItems(defaultBucketQuery, persistendBucketFilter, searchOptions);

                        if (searchResponse != null)
                        {
                            returnList.AddRange(searchResponse.ResponseItems);

                            totalProductCount = searchResponse.TotalItemCount;
                            totalPageCount    = searchResponse.TotalPageCount;
                            facets            = searchResponse.Facets;
                        }
                    }
                    catch (Exception ex)
                    {
                        error = ex.Message;
                    }

                    return(new SearchResults(returnList, totalProductCount, totalPageCount, searchOptions.StartPageIndex, facets));
                }

                var childProducts = GetChildProducts(productSearchOptions, dataSource).SearchResultItems;
                return(new SearchResults(childProducts, totalProductCount, totalPageCount, productSearchOptions.StartPageIndex, new List <CommerceQueryFacet>()));
            }

            return(null);
        }
Example #10
0
        public StockInfoListBaseJsonResult GetCurrentProductStockInfo(string productId)
        {
            var currentProductItem = SearchNavigation.GetProduct(productId, CurrentCatalog.Name);

            productId = currentProductItem.Name;
            var catalogName = currentProductItem[CommerceTemplates.CommerceGenerated.GeneralCategory.Fields.CatalogName];
            var products    = new List <CommerceInventoryProduct>();

            if (currentProductItem.HasChildren)
            {
                foreach (Item item in currentProductItem.Children)
                {
                    products.Add(new CommerceInventoryProduct
                    {
                        ProductId   = productId,
                        CatalogName = catalogName,
                        VariantId   = item.Name
                    });
                }
            }
            else
            {
                products.Add(new CommerceInventoryProduct {
                    ProductId = productId, CatalogName = catalogName
                });
            }

            var response = _inventoryManager.GetStockInformation(this.CurrentStorefront, products, StockDetailsLevel.All);
            var result   = new StockInfoListBaseJsonResult(response.ServiceProviderResult);

            if (response.Result == null)
            {
                return(result);
            }

            result.Initialize(response.Result);
            var stockInfo = response.Result.FirstOrDefault();

            if (stockInfo != null)
            {
                _inventoryManager.VisitedProductStockStatus(this.CurrentStorefront, stockInfo, string.Empty);
            }

            return(result);
        }
        /// <summary>
        /// Gets a <see cref="SearchResults" /> that represents the site content search results.
        /// </summary>
        /// <param name="searchOptions">The search options.</param>
        /// <param name="searchKeyword">The search keyword.</param>
        /// <param name="rendering">The rendering.</param>
        /// <returns>The search results.</returns>
        protected virtual SearchResults GetSiteContentSearchResults(CommerceSearchOptions searchOptions, string searchKeyword, Rendering rendering)
        {
            if (this.CurrentSiteContext.Items[CurrentSearchContentResultsKeyName] != null)
            {
                return((SearchResults)this.CurrentSiteContext.Items[CurrentSearchContentResultsKeyName]);
            }

            var searchResults  = new SearchResults();
            var searchResponse = SearchNavigation.SearchSiteByKeyword(searchKeyword, searchOptions);

            if (searchResponse != null)
            {
                searchResults = new SearchResults(searchResponse.ResponseItems, searchResponse.TotalItemCount, searchResponse.TotalPageCount, searchOptions.StartPageIndex, searchResponse.Facets);
            }

            this.CurrentSiteContext.Items[CurrentSearchContentResultsKeyName] = searchResults;
            return(searchResults);
        }
        /// <summary>
        /// The action for rendering the product view
        /// </summary>
        /// <returns>The product view</returns>
        public ActionResult Product()
        {
            //Wild card pages are named "*"
            if (this.Item.Name == "*")
            {
                // This is a Wild Card
                var productViewModel = this.GetWildCardProductViewModel();
                return(this.View(CurrentRenderingView, productViewModel));
            }

            //Special handling for gift card
            if (this.Item.Name.ToLower(CultureInfo.InvariantCulture) == ProductItemResolver.BuyGiftCardUrlRoute)
            {
                this.Item = SearchNavigation.GetProduct(StorefrontManager.CurrentStorefront.GiftCardProductId, CurrentCatalog.Name);
            }

            return(this.View(CurrentRenderingView, GetProductViewModel(this.Item, CurrentRendering)));
        }
        /// <summary>
        /// This method returns child products for this category
        /// </summary>
        /// <param name="searchOptions">The options to perform the search with</param>
        /// <param name="categoryItem">The category item whose children to retrieve</param>
        /// <returns>A list of child products</returns>
        protected SearchResults GetChildProducts(CommerceSearchOptions searchOptions, Item categoryItem)
        {
            IEnumerable <CommerceQueryFacet> facets = null;
            var returnList        = new List <Item>();
            var totalPageCount    = 0;
            var totalProductCount = 0;

            if (RenderingContext.Current.Rendering.Item != null)
            {
                SearchResponse searchResponse = null;
                if (CatalogUtility.IsItemDerivedFromCommerceTemplate(categoryItem, CommerceConstants.KnownTemplateIds.CommerceDynamicCategoryTemplate) || categoryItem.TemplateName == "Commerce Named Search")
                {
                    try
                    {
                        var defaultBucketQuery     = categoryItem[CommerceConstants.KnownSitecoreFieldNames.DefaultBucketQuery];
                        var persistendBucketFilter = categoryItem[CommerceConstants.KnownSitecoreFieldNames.PersistentBucketFilter];
                        persistendBucketFilter = CleanLanguageFromFilter(persistendBucketFilter);
                        searchResponse         = SearchNavigation.SearchCatalogItems(defaultBucketQuery, persistendBucketFilter, searchOptions);
                    }
                    catch (Exception ex)
                    {
                        Helpers.LogException(ex, this);
                    }
                }
                else
                {
                    searchResponse = SearchNavigation.GetCategoryProducts(categoryItem.ID, searchOptions);
                }

                if (searchResponse != null)
                {
                    returnList.AddRange(searchResponse.ResponseItems);

                    totalProductCount = searchResponse.TotalItemCount;
                    totalPageCount    = searchResponse.TotalPageCount;
                    facets            = searchResponse.Facets;
                }
            }

            var results = new SearchResults(returnList, totalProductCount, totalPageCount, searchOptions.StartPageIndex, facets);

            return(results);
        }
        /// <summary>
        /// This method returns child categories for this category
        /// </summary>
        /// <param name="searchOptions">The options to perform the search with</param>
        /// <param name="categoryItem">The category item whose children to retrieve</param>
        /// <returns>A list of child categories</returns>
        protected CategorySearchResults GetChildCategories(CommerceSearchOptions searchOptions, Item categoryItem)
        {
            var returnList         = new List <Item>();
            var totalPageCount     = 0;
            var totalCategoryCount = 0;

            if (Item != null)
            {
                var searchResponse = SearchNavigation.GetCategoryChildCategories(categoryItem.ID, searchOptions);

                returnList.AddRange(searchResponse.ResponseItems);

                totalCategoryCount = searchResponse.TotalItemCount;
                totalPageCount     = searchResponse.TotalPageCount;
            }

            var results = new CategorySearchResults(returnList, totalCategoryCount, totalPageCount, searchOptions.StartPageIndex, new List <FacetCategory>());

            return(results);
        }
Example #15
0
        /// <summary>
        /// Checks to see if there is a catalog item that maps to the current id
        /// </summary>
        /// <param name="itemId">The ID of the catalog item.</param>
        /// <param name="catalogName">The name of the catalog that contains the catalog item.</param>
        /// <param name="isProduct">Specifies whether the item is a product.</param>
        /// <returns>An item if found, otherwise null</returns>
        public static Sitecore.Data.Items.Item ResolveCatalogItem(string itemId, string catalogName, bool isProduct)
        {
            Sitecore.Data.Items.Item foundItem = null;

            // If we make it here, the right route was used, but might have an empty value
            if (!string.IsNullOrEmpty(itemId))
            {
                var            cachekey      = "FriendlyUrl-" + itemId + "-" + catalogName;
                ICacheProvider cacheProvider = CommerceTypeLoader.GetCacheProvider(CommerceConstants.KnownCacheNames.FriendlyUrlsCache);
                var            id            = cacheProvider.GetData <ID>(CommerceConstants.KnownCachePrefixes.Sitecore, CommerceConstants.KnownCacheNames.FriendlyUrlsCache, cachekey);

                if (ID.IsNullOrEmpty(id) || id == ID.Undefined)
                {
                    if (isProduct)
                    {
                        foundItem = SearchNavigation.GetProduct(itemId, catalogName);
                    }
                    else
                    {
                        foundItem = SearchNavigation.GetCategory(itemId, catalogName);
                    }

                    if (foundItem != null)
                    {
                        cacheProvider.AddData <ID>(CommerceConstants.KnownCachePrefixes.Sitecore, CommerceConstants.KnownCacheNames.FriendlyUrlsCache, cachekey, foundItem.ID);
                    }
                    else
                    {
                        cacheProvider.AddData <ID>(CommerceConstants.KnownCachePrefixes.Sitecore, CommerceConstants.KnownCacheNames.FriendlyUrlsCache, cachekey, ID.Undefined);
                    }
                }
                else if (id != ID.Undefined && id != ID.Null)
                {
                    foundItem = Context.Database.GetItem(id);
                }
            }

            return(foundItem);
        }
        /// <summary>
        /// Get category by id
        /// </summary>
        /// <param name="categoryId">The category identifier.</param>
        /// <returns>The category.</returns>
        public Category GetCategory(string categoryId)
        {
            var categoryItem = SearchNavigation.GetCategory(categoryId, CurrentCatalog.Name);

            return(GetCategory(categoryItem));
        }