protected virtual void GetSortParameters(CategorySearchInformation categorySearchInformation, ref string sortField, ref SortDirection?sortOrder)
        {
            if (!string.IsNullOrWhiteSpace(sortField))
            {
                return;
            }

            var sortFields = categorySearchInformation.SortFields;

            if (sortFields == null || sortFields.Count <= 0)
            {
                return;
            }

            sortField = sortFields[0].Name;
            sortOrder = (SortDirection?)CommerceConstants.SortDirection.Asc;
        }
        protected virtual int GetDefaultItemsPerPage(
            int?pageSize,
            CategorySearchInformation searchInformation)
        {
            Assert.ArgumentNotNull(searchInformation, nameof(searchInformation));
            int defaultValue = StorefrontContext.CurrentStorefront.DefaultItemsPerPage;

            if (defaultValue <= 0)
            {
                defaultValue = searchInformation.ItemsPerPage;
            }
            if (defaultValue <= 0)
            {
                defaultValue = 12;
            }
            return(pageSize.GetValueOrDefault(defaultValue));
        }
        protected void ApplySortOptions(
            ProductListResultModel model,
            CommerceSearchOptions commerceSearchOptions,
            CategorySearchInformation searchInformation)
        {
            Assert.ArgumentNotNull(model, nameof(model));
            Assert.ArgumentNotNull(commerceSearchOptions, nameof(commerceSearchOptions));
            Assert.ArgumentNotNull(searchInformation, nameof(searchInformation));

            if ((searchInformation.SortFields == null) || !searchInformation.SortFields.Any())
            {
                return;
            }

            var sortOptions = new List <SortOptionModel>();

            foreach (var sortField in searchInformation.SortFields)
            {
                var isSelected = sortField.Name.Equals(commerceSearchOptions.SortField);

                var sortOptionAsc = new SortOptionModel
                {
                    Name          = sortField.Name,
                    DisplayName   = sortField.DisplayName,
                    SortDirection = SortDirection.Asc,
                    IsSelected    = isSelected && (commerceSearchOptions.SortDirection == CommerceConstants.SortDirection.Asc)
                };

                sortOptions.Add(sortOptionAsc);

                var sortOptionDesc = new SortOptionModel
                {
                    Name          = sortField.Name,
                    DisplayName   = sortField.DisplayName,
                    SortDirection = SortDirection.Desc,
                    IsSelected    = isSelected && (commerceSearchOptions.SortDirection == CommerceConstants.SortDirection.Desc)
                };

                sortOptions.Add(sortOptionDesc);
            }

            model.SortOptions = sortOptions;
        }
        protected virtual void GetSortParameters(
            CategorySearchInformation categorySearchInformation,
            ref string sortField,
            ref SortDirection?sortOrder)
        {
            Assert.ArgumentNotNull(categorySearchInformation, nameof(categorySearchInformation));
            if (!string.IsNullOrWhiteSpace(sortField))
            {
                return;
            }
            IList <CommerceQuerySort> sortFields = categorySearchInformation.SortFields;

            if (sortFields == null || sortFields.Count <= 0)
            {
                return;
            }
            sortField = sortFields[0].Name;
            sortOrder = new SortDirection?(SortDirection.Asc);
        }
        public ProductListResultModel GetProductList(
            IVisitorContext visitorContext,
            string currentItemId,
            string currentCatalogItemId,
            string searchKeyword,
            int?pageNumber,
            NameValueCollection facetValues,
            string sortField,
            int?pageSize,
            SortDirection?sortDirection)
        {
            Assert.ArgumentNotNull(visitorContext, nameof(visitorContext));

            var model = new ProductListResultModel();

            Item specifiedCatalogItem = !string.IsNullOrEmpty(currentCatalogItemId) ? Sitecore.Context.Database.GetItem(currentCatalogItemId) : null;
            Item currentCatalogItem   = specifiedCatalogItem ?? this.storefrontContext.CurrentCatalog.Item;

            model.CurrentCatalogItemId = currentCatalogItem.ID.Guid.ToString("D");

            // var currentItem = Sitecore.Context.Database.GetItem(currentItemId);

            // this.siteContext.CurrentCategoryItem = currentCatalogItem;
            // this.siteContext.CurrentItem = currentItem;
            CategorySearchInformation searchInformation = this.searchInformationProvider.GetCategorySearchInformation(currentCatalogItem);

            this.GetSortParameters(searchInformation, ref sortField, ref sortDirection);

            int itemsPerPage          = this.settingsProvider.GetDefaultItemsPerPage(pageSize, searchInformation);
            var commerceSearchOptions = new CommerceSearchOptions(itemsPerPage, pageNumber.GetValueOrDefault(0));

            this.UpdateOptionsWithFacets(searchInformation.RequiredFacets, facetValues, commerceSearchOptions);
            this.UpdateOptionsWithSorting(sortField, sortDirection, commerceSearchOptions);

            SearchResults        childProducts     = this.GetChildProducts(searchKeyword, commerceSearchOptions, specifiedCatalogItem);
            IList <ProductModel> productEntityList = this.AdjustProductPriceAndStockStatus(visitorContext, childProducts, currentCatalogItem);

            model.Initialize(commerceSearchOptions, childProducts, productEntityList);
            this.ApplySortOptions(model, commerceSearchOptions, searchInformation);

            return(model);
        }