Beispiel #1
0
        protected virtual async Task LoadProductVendorsAsync(List <Product> products, WorkContext workContext)
        {
            var vendorIds = products.Where(p => !string.IsNullOrEmpty(p.VendorId)).Select(p => p.VendorId).Distinct().ToArray();

            if (!vendorIds.IsNullOrEmpty())
            {
                var vendors = await _customerService.GetVendorsByIdsAsync(workContext.CurrentStore, workContext.CurrentLanguage, vendorIds);

                foreach (var product in products)
                {
                    product.Vendor = vendors.FirstOrDefault(v => v != null && v.Id == product.VendorId);
                    if (product.Vendor != null)
                    {
                        product.Vendor.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos, @params) =>
                        {
                            var criteria = new ProductSearchCriteria
                            {
                                VendorId      = product.VendorId,
                                PageNumber    = pageNumber,
                                PageSize      = pageSize,
                                ResponseGroup = workContext.CurrentProductSearchCriteria.ResponseGroup & ~ItemResponseGroup.ItemWithVendor,
                                SortBy        = SortInfo.ToString(sortInfos),
                            };
                            if (@params != null)
                            {
                                criteria.CopyFrom(@params);
                            }
                            var searchResult = SearchProducts(criteria);
                            return(searchResult.Products);
                        }, 1, ProductSearchCriteria.DefaultPageSize);
                    }
                }
            }
        }
Beispiel #2
0
        public static Task WithVendorsAsync(this IWorkContextBuilder builder, Store store, Language language)
        {
            var serviceProvider = builder.HttpContext.RequestServices;
            var customerService = serviceProvider.GetRequiredService <IMemberService>();
            var catalogService  = serviceProvider.GetRequiredService <ICatalogService>();

            Func <int, int, IEnumerable <SortInfo>, IPagedList <Vendor> > factory = (pageNumber, pageSize, sortInfos) =>
            {
                var vendors = customerService.SearchVendors(store, language, null, pageNumber, pageSize, sortInfos);
                foreach (var vendor in vendors)
                {
                    vendor.Products = new MutablePagedList <Product>((pageNumber2, pageSize2, sortInfos2, @params) =>
                    {
                        var vendorProductsSearchCriteria = new ProductSearchCriteria
                        {
                            VendorId      = vendor.Id,
                            PageNumber    = pageNumber2,
                            PageSize      = pageSize2,
                            ResponseGroup = builder.WorkContext.CurrentProductSearchCriteria.ResponseGroup & ~ItemResponseGroup.ItemWithVendor,
                            SortBy        = SortInfo.ToString(sortInfos2),
                        };
                        if (@params != null)
                        {
                            vendorProductsSearchCriteria.CopyFrom(@params);
                        }
                        var searchResult = catalogService.SearchProducts(vendorProductsSearchCriteria);
                        return(searchResult.Products);
                    }, 1, ProductSearchCriteria.DefaultPageSize);
                }
                return(vendors);
            };

            return(builder.WithVendorsAsync(() => new MutablePagedList <Vendor>(factory, 1, VendorSearchCriteria.DefaultPageSize)));
        }
        public async Task <ActionResult> VendorDetails(string vendorId)
        {
            var vendor = (await _customerService.GetVendorsByIdsAsync(base.WorkContext.CurrentStore, base.WorkContext.CurrentLanguage, vendorId)).FirstOrDefault();

            if (vendor != null)
            {
                vendor.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos, @params) =>
                {
                    var criteria = new ProductSearchCriteria
                    {
                        VendorId      = vendorId,
                        PageNumber    = pageNumber,
                        PageSize      = pageSize,
                        SortBy        = SortInfo.ToString(sortInfos),
                        ResponseGroup = base.WorkContext.CurrentProductSearchCriteria.ResponseGroup
                    };
                    if (@params != null)
                    {
                        criteria.CopyFrom(@params);
                    }
                    var searchResult = _catalogService.SearchProducts(criteria);
                    return(searchResult.Products);
                }, 1, ProductSearchCriteria.DefaultPageSize);

                WorkContext.CurrentPageSeo = vendor.SeoInfo;
                WorkContext.CurrentVendor  = vendor;

                return(View("vendor", WorkContext));
            }

            return(NotFound());
        }
Beispiel #4
0
        public static Task WithCatalogsAsync(this IWorkContextBuilder builder)
        {
            var serviceProvider = builder.HttpContext.RequestServices;
            var catalogService  = serviceProvider.GetRequiredService <ICatalogService>();
            var workContext     = builder.WorkContext;
            var defaultSort     = "priority-descending;name-ascending";

            //Initialize catalog search criteria
            var productSearchcriteria = new ProductSearchCriteria(workContext.CurrentLanguage, workContext.CurrentCurrency, workContext.QueryString)
            {
                UserGroups = workContext.CurrentUser?.Contact?.UserGroups ?? new List <string>()
            };

            if (string.IsNullOrEmpty(productSearchcriteria.SortBy))
            {
                productSearchcriteria.SortBy = defaultSort;
            }
            workContext.CurrentProductSearchCriteria = productSearchcriteria;
            //Initialize product response group.
            //TODO: Need to find possibility to set this response group in theme
            workContext.CurrentProductResponseGroup = EnumUtility.SafeParse(workContext.QueryString.Get("resp_group"), ItemResponseGroup.ItemMedium | ItemResponseGroup.ItemWithPrices | ItemResponseGroup.ItemWithVendor | ItemResponseGroup.ItemAssociations);

            //This line make delay categories loading initialization (categories can be evaluated on view rendering time)
            workContext.Categories = new MutablePagedList <Category>((pageNumber, pageSize, sortInfos, @params) =>
            {
                var criteria = new CategorySearchCriteria(workContext.CurrentLanguage)
                {
                    PageNumber    = pageNumber,
                    PageSize      = pageSize,
                    ResponseGroup = CategoryResponseGroup.Small
                };

                if (@params != null)
                {
                    criteria.CopyFrom(@params);
                }
                if (string.IsNullOrEmpty(criteria.SortBy) && !sortInfos.IsNullOrEmpty())
                {
                    criteria.SortBy = SortInfo.ToString(sortInfos);
                }
                var result = catalogService.SearchCategories(criteria);
                foreach (var category in result)
                {
                    category.Products = new MutablePagedList <Product>((pageNumber2, pageSize2, sortInfos2, params2) =>
                    {
                        var productSearchCriteria = new ProductSearchCriteria(workContext.CurrentLanguage, workContext.CurrentCurrency)
                        {
                            PageNumber    = pageNumber2,
                            PageSize      = pageSize2,
                            Outline       = category.Outline,
                            ResponseGroup = workContext.CurrentProductSearchCriteria.ResponseGroup,
                            UserGroups    = workContext.CurrentUser?.Contact?.UserGroups ?? new List <string>()
                        };
                        if (params2 != null)
                        {
                            productSearchCriteria.CopyFrom(params2);
                        }
                        //criteria.CategoryId = category.Id;
                        if (string.IsNullOrEmpty(productSearchCriteria.SortBy) && !sortInfos2.IsNullOrEmpty())
                        {
                            productSearchCriteria.SortBy = SortInfo.ToString(sortInfos2);
                        }

                        return(catalogService.SearchProducts(productSearchCriteria).Products);
                    }, 1, ProductSearchCriteria.DefaultPageSize);
                }
                return(result);
            }, 1, CategorySearchCriteria.DefaultPageSize);

            //This line make delay products loading initialization (products can be evaluated on view rendering time)
            workContext.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos, @params) =>
            {
                var criteria        = workContext.CurrentProductSearchCriteria.Clone() as ProductSearchCriteria;
                criteria.PageNumber = pageNumber;
                criteria.PageSize   = pageSize;
                if (string.IsNullOrEmpty(criteria.SortBy) && !sortInfos.IsNullOrEmpty())
                {
                    criteria.SortBy = SortInfo.ToString(sortInfos);
                }
                if (@params != null)
                {
                    criteria.CopyFrom(@params);
                }
                var result = catalogService.SearchProducts(criteria);
                //Need change ProductSearchResult with preserve reference because Scriban engine keeps this reference and use new operator will create the new
                //object that doesn't tracked by Scriban
                builder.WorkContext.ProductSearchResult.Aggregations = result.Aggregations;
                builder.WorkContext.ProductSearchResult.Products     = result.Products;
                return(result.Products);
            }, 1, ProductSearchCriteria.DefaultPageSize);

            builder.WorkContext.ProductSearchResult = new CatalogSearchResult(productSearchcriteria)
            {
                Products = builder.WorkContext.Products
            };

            return(Task.CompletedTask);
        }