private IQueryable <Ucommerce.Search.Models.Product> GetProductsQuery(Ucommerce.Search.Models.Category category,
                                                                              string searchTerm)
        {
            if (this.isManualSelectionMode)
            {
                // NOTE: The int values will go away soon but the picker still saves as ints at the moment so we need to convert them
                var productIds  = this.productIds?.Split(',').Select(Int32.Parse).ToList() ?? new List <int>();
                var categoryIds = this.categoryIds?.Split(',').Select(Int32.Parse).ToList() ?? new List <int>();

                var productGuids = Ucommerce.EntitiesV2.Product.Find(p => productIds.Contains(p.ProductId))
                                   .Select(p => p.Guid).ToList();
                var categoryGuids = Ucommerce.EntitiesV2.Category.Find(c => categoryIds.Contains(c.CategoryId))
                                    .Select(c => c.Guid).ToList();

                return(ApplyManualSelection(productGuids, categoryGuids));
            }

            if (string.IsNullOrWhiteSpace(searchTerm) && category == null && this.enableCategoryFallback == true)
            {
                var categoryIds   = this.fallbackCategoryIds?.Split(',').Select(Int32.Parse).ToList() ?? new List <int>();
                var categoryGuids = Ucommerce.EntitiesV2.Category.Find(c => categoryIds.Contains(c.CategoryId))
                                    .Select(c => c.Guid).ToList();
                return(ApplyManualSelection(new List <Guid>(), categoryGuids));
            }

            return(ApplyAutoSelection(category, searchTerm));
        }
        private IQueryable <Ucommerce.Search.Models.Product> ApplyAutoSelection(
            Ucommerce.Search.Models.Category currentCategory, string searchTerm)
        {
            var facets = HttpContext.Current.Request.QueryString.ToFacets();

            ISearch <Product> matchingProducts = ProductIndex.Find <Ucommerce.Search.Models.Product>()
                                                 .Where(p => p.ProductType != ProductType.Variant);

            if (facets.Count > 0)
            {
                matchingProducts.Where(facets.ToFacetDictionary());
            }

            if (currentCategory != null)
            {
                matchingProducts = matchingProducts.Where(p => p.Categories.Contains(currentCategory.Guid));
            }

            if (!string.IsNullOrWhiteSpace(searchTerm))
            {
                matchingProducts = matchingProducts.Where(p =>
                                                          p.Sku.Contains(searchTerm) ||
                                                          p.Name.Contains(searchTerm) ||
                                                          p.DisplayName.Contains(searchTerm) ||
                                                          p.ShortDescription.Contains(searchTerm) ||
                                                          p.LongDescription.Contains(searchTerm)
                                                          );
            }

            return(matchingProducts.ToList().AsQueryable());
        }
        private IList <ProductDTO> MapProducts(IList <Ucommerce.Search.Models.Product> products,
                                               Ucommerce.Search.Models.Category category, bool openInSamePage, Guid detailPageId)
        {
            var result             = new List <ProductDTO>();
            var imageService       = ObjectFactory.Instance.Resolve <IImageService>();
            var currentCatalog     = CatalogContext.CurrentCatalog;
            var productsPrices     = CatalogLibrary.CalculatePrices(products.Select(x => x.Guid).ToList());
            var productDefinitions = Ucommerce.EntitiesV2.ProductDefinition.All().ToList();

            foreach (var product in products)
            {
                var singleProductPrice = productsPrices.Items.FirstOrDefault(x => x.ProductGuid == product.Guid);

                decimal price    = 0;
                decimal discount = 0;

                if (singleProductPrice != null)
                {
                    price    = singleProductPrice.PriceExclTax;
                    discount = singleProductPrice.DiscountExclTax;
                    if (currentCatalog.ShowPricesIncludingTax)
                    {
                        price    = singleProductPrice.PriceInclTax;
                        discount = singleProductPrice.DiscountInclTax;
                    }
                }

                var definition      = productDefinitions.FirstOrDefault(pd => pd.Guid == product.ProductDefinition);
                var isProductFamily = definition.IsProductFamily();

                var productViewModel = new ProductDTO()
                {
                    Sku        = product.Sku,
                    VariantSku = product.VariantSku,
                    Price      = new Money(price, CatalogContext.CurrentPriceGroup.CurrencyISOCode).ToString(),
                    Discount   = discount > 0
                                                ? new Money(discount, CatalogContext.CurrentPriceGroup.CurrencyISOCode).ToString()
                                                : "",
                    DisplayName            = product.DisplayName,
                    ThumbnailImageMediaUrl = product.ThumbnailImageUrl,
                    ProductUrl             = this.GetProductUrl(category, product, openInSamePage, detailPageId),
                    IsSellableProduct      = (isProductFamily && product.ParentProduct != null) ||
                                             (!isProductFamily && product.ParentProduct == null),
                    IsAddableToCart = !isProductFamily
                };

                result.Add(productViewModel);
            }

            return(result);
        }
        private string GetPagingUrlTemplate(Ucommerce.Search.Models.Category category)
        {
            string url;

            if (SystemManager.CurrentHttpContext.Request.Url != null)
            {
                var queryParams = HttpUtility.ParseQueryString(SystemManager.CurrentHttpContext.Request.Url.Query);
                queryParams[PAGER_QUERY_STRING_KEY] = "{0}";

                var queryParamsRaw = HttpUtility.UrlDecode(queryParams.ToQueryString(true));
                url = string.Concat(SystemManager.CurrentHttpContext.Request.Url.LocalPath, queryParamsRaw);
            }
            else
            {
                url = UrlResolver.GetAbsoluteUrl(SiteMapBase.GetActualCurrentNode().Url + "?{0}");
            }

            return(url);
        }
        public virtual string GetProductUrl(Ucommerce.Search.Models.Category category,
                                            Ucommerce.Search.Models.Product product, bool openInSamePage, Guid detailPageId)
        {
            var baseUrl = string.Empty;

            if (openInSamePage)
            {
                baseUrl = UrlResolver.GetCurrentPageNodeUrl();
            }
            else
            {
                baseUrl = UrlResolver.GetPageNodeUrl(detailPageId);
            }

            string catUrl;
            var    productCategory = category?.Guid ?? product?.Categories.FirstOrDefault();

            if (productCategory == null)
            {
                catUrl = CategoryModel.DefaultCategoryName;
            }
            else
            {
                catUrl = CategoryModel.GetCategoryPath(CatalogLibrary.GetCategory(productCategory));
            }

            var    rawtUrl     = string.Format("{0}/p/{1}", catUrl, product.Slug);
            string relativeUrl = string.Concat(VirtualPathUtility.RemoveTrailingSlash(baseUrl), "/", rawtUrl);

            string url;

            if (SystemManager.CurrentHttpContext.Request.Url != null)
            {
                url = UrlPath.ResolveUrl(relativeUrl, true);
            }
            else
            {
                url = UrlResolver.GetAbsoluteUrl(relativeUrl);
            }

            return(url);
        }