public static List <Product> FindProductsByCategory(int categoryId, ProductSortByField sortBy) { ProductQuery p = new ProductQuery("p"); ProductCategoryQuery pc = new ProductCategoryQuery("pc"); vProductsSoldCountsQuery productsSold = new vProductsSoldCountsQuery("productsSold"); p.Select(p); p.InnerJoin(pc).On(p.Id == pc.ProductId); p.LeftJoin(productsSold).On(p.Id == productsSold.ProductId); p.Where(p.IsActive == true); p.Where(pc.CategoryId == categoryId); if (!string.IsNullOrEmpty(sortBy.Field)) { p.OrderBy(sortBy.Field, sortBy.SortDirection == SortDirection.ASC ? esOrderByDirection.Ascending : esOrderByDirection.Descending); } //string sql = p.Parse(); ProductCollection products = new ProductCollection(); products.Load(p); return(products.Where(z => z.IsViewable == true).ToList()); }
private void LoadCategoryProducts() { int pageIndex = WA.Parser.ToInt(Request.QueryString["pg"]).GetValueOrDefault(1) - 1; int pageSize = WA.Parser.ToInt(StoreContext.CurrentStore.GetSetting(StoreSettingNames.CatalogMaxResultsPerPage)).GetValueOrDefault(100); string defaultSortName = StoreContext.CurrentStore.GetSetting(StoreSettingNames.CatalogDefaultSortOrder) ?? ""; ProductSortByField defaultSortField = sortByFields.Find(f => f.DisplayName == defaultSortName); sortByField = ProductSortByField.FromString(Request.QueryString["sb"]) ?? defaultSortField ?? sortByFields[0]; List <Product> products = category.GetProducts(sortByField); if (products.Count > 0) { productList = new PagedList <Product>(products, pageIndex, pageSize); rptCategoryProducts.DataSource = productList; rptCategoryProducts.DataBind(); if (products.Count > pageSize) { RenderPaginationLinks(productList); } } else { pnlCategoryProductResults.Visible = false; } pnlNoResults.Visible = !pnlCategoryProductResults.Visible; }
public List <Product> GetProducts(ProductSortByField productSortByField) { if (this.Id.HasValue) { return(ProductCollection.FindProductsByCategory(this.Id.Value, productSortByField)); } else { return(null); } }
private void LoadFeaturedProducts() { var sortField = ProductSortByField.FromString(Convert.ToString(Settings[FeaturedSettings.SortBy])) ?? new ProductSortByField() { Field = "Name", SortDirection = SortDirection.ASC }; var categoryIds = Convert.ToString(Settings[FeaturedSettings.CategoryFilterCategoryIds]).Split(',').Select(x => WA.Parser.ToInt(x).GetValueOrDefault(-1)).ToList(); bool matchAll = Convert.ToString(Settings[FeaturedSettings.CategoryFilterMethod]).ToLower() == "all"; var products = ProductCollection.FindProductsByCategories(categoryIds, sortField, matchAll); int maxProducts = WA.Parser.ToInt(Settings[FeaturedSettings.MaxNumProducts]).GetValueOrDefault(25); if (products.Count > maxProducts) { products = products.Take(maxProducts).ToList(); } string tplHeader = Settings[FeaturedSettings.TemplateHeader].ToString(); string tplProduct = Settings[FeaturedSettings.TemplateProduct].ToString(); string tplFooter = Settings[FeaturedSettings.TemplateFooter].ToString(); string tplNoResults = Settings[FeaturedSettings.TemplateNoResults].ToString(); if (products.Count == 0) { litTemplateOutput.Text = tplHeader + tplNoResults + tplFooter; } else { var productTokenizer = new ProductTokenValueProvider(StoreContext); var templateProcessor = new TemplateProcessor(productTokenizer); StringBuilder productsHtml = new StringBuilder(products.Count); foreach (var p in products) { productTokenizer.Product = p; productsHtml.Append(templateProcessor.ProcessTemplate(tplProduct)); } litTemplateOutput.Text = tplHeader + productsHtml.ToString() + tplFooter; } }
public static List <Product> FindProductsByCategories(IList <int> categoryIds, ProductSortByField sortBy, bool matchAllCategories) { if (categoryIds.Count > 0) { ProductQuery p = new ProductQuery("p"); ProductCategoryQuery pc = new ProductCategoryQuery("pc"); vProductsSoldCountsQuery productsSold = new vProductsSoldCountsQuery("productsSold"); //p.es.CountAll = true; //p.es.CountAllAlias = "TotalResultCount"; //p.es.PageNumber = 1; //p.es.PageSize = 2; p.es.Distinct = true; p.Select(p); p.InnerJoin(pc).On(p.Id == pc.ProductId); p.LeftJoin(productsSold).On(p.Id == productsSold.ProductId); p.Where(p.IsActive == true); if (matchAllCategories) { ProductCategoryQuery pcAllCats = new ProductCategoryQuery("pcAll"); pcAllCats.Select(pcAllCats.ProductId) .Where(pcAllCats.CategoryId.In(categoryIds.ToArray())) .GroupBy(pcAllCats.ProductId) .Having(pcAllCats.ProductId.Count() == categoryIds.Count); p.Where(p.Id.In(pcAllCats)); } else { p.Where(pc.CategoryId.In(categoryIds.ToArray())); } if (!string.IsNullOrEmpty(sortBy.Field)) { p.OrderBy(sortBy.Field, sortBy.SortDirection == SortDirection.ASC ? esOrderByDirection.Ascending : esOrderByDirection.Descending); } string sql = p.Parse(); ProductCollection products = new ProductCollection(); products.Load(p); return(products.Where(z => z.IsViewable == true).ToList()); } return(new List <Product>()); }