Esempio n. 1
0
        private IList <FullTextSearchResultDTO> ConvertToFullTextSearchResultModel(IList <Product> products, Guid?productDetailsPageId)
        {
            var fullTextSearchResultModels = new List <FullTextSearchResultDTO>();

            var            currency       = UCommerce.Runtime.SiteContext.Current.CatalogContext.CurrentPriceGroup.Currency;
            var            productsPrices = UCommerce.Api.CatalogLibrary.CalculatePrice(products.Select(x => x.Guid).ToList()).Items;
            ProductCatalog catalog        = UCommerce.Api.CatalogLibrary.GetCatalog();



            var culture = System.Threading.Thread.CurrentThread.CurrentUICulture;

            foreach (var product in products)
            {
                string catUrl = CategoryModel.DefaultCategoryName;

                if (product.CategoryIds != null && product.CategoryIds.Any())
                {
                    var productCategory = catalog.Categories
                                          .Where(c => c.CategoryId == product.CategoryIds.FirstOrDefault())
                                          .FirstOrDefault();

                    if (productCategory != null)
                    {
                        catUrl = CategoryModel.GetCategoryPath(productCategory);
                    }
                }

                string detailsPageUrl = string.Empty;

                if (productDetailsPageId != null && productDetailsPageId.Value != Guid.Empty)
                {
                    detailsPageUrl = Pages.UrlResolver.GetPageNodeUrl(productDetailsPageId.Value);

                    if (!detailsPageUrl.EndsWith("/"))
                    {
                        detailsPageUrl += "/";
                    }

                    detailsPageUrl += catUrl + "/" + product.Id.ToString();
                    detailsPageUrl  = Pages.UrlResolver.GetAbsoluteUrl(detailsPageUrl);
                }

                if (string.IsNullOrWhiteSpace(detailsPageUrl))
                {
                    var entityProduct = this.productRepository.Get(product.Id);
                    detailsPageUrl = CatalogLibrary.GetNiceUrlForProduct(entityProduct);
                }

                var fullTestSearchResultModel = new FullTextSearchResultDTO()
                {
                    ThumbnailImageUrl = product.ThumbnailImageUrl,
                    Name  = product.Name,
                    Url   = detailsPageUrl,
                    Price = new Money(productsPrices.First(x => x.ProductGuid == product.Guid).PriceInclTax, currency).ToString(),
                };

                fullTextSearchResultModels.Add(fullTestSearchResultModel);
            }

            return(fullTextSearchResultModels);
        }
        private IList <FullTextSearchResultDTO> ConvertToFullTextSearchResultModel(ResultSet <Product> products, Guid?productDetailsPageId)
        {
            var fullTextSearchResultModels = new List <FullTextSearchResultDTO>();

            if (products == null || !products.Any())
            {
                return(fullTextSearchResultModels);
            }

            var currencyIsoCode = CatalogContext.CurrentPriceGroup.CurrencyISOCode;
            var productsPrices  = CatalogLibrary.CalculatePrices(products.Select(x => x.Guid).ToList()).Items;
            var catalog         = CatalogContext.CurrentCatalog;

            foreach (var product in products)
            {
                string catUrl = string.Empty;

                if (product.Categories != null && product.Categories.Any())
                {
                    var productCategoryId = catalog.Categories.FirstOrDefault(x => product.Categories.Any(c => c == x));
                    if (productCategoryId != Guid.Empty)
                    {
                        var category = CatalogContext.CurrentCategories.FirstOrDefault(x => x.Guid == productCategoryId);
                        catUrl = CategoryModel.GetCategoryPath(category);
                    }
                }

                if (string.IsNullOrWhiteSpace(catUrl))
                {
                    catUrl = CategoryModel.DefaultCategoryName;
                }

                string detailsPageUrl = string.Empty;
                if (productDetailsPageId != null && productDetailsPageId.Value != Guid.Empty)
                {
                    detailsPageUrl = Pages.UrlResolver.GetPageNodeUrl(productDetailsPageId.Value);

                    if (!detailsPageUrl.EndsWith("/"))
                    {
                        detailsPageUrl += "/";
                    }

                    detailsPageUrl += $"{catUrl}/p/{product.Slug}";
                    detailsPageUrl  = Pages.UrlResolver.GetAbsoluteUrl(detailsPageUrl);
                }

                if (string.IsNullOrWhiteSpace(detailsPageUrl))
                {
                    detailsPageUrl = UrlService.GetUrl(CatalogContext.CurrentCatalog, product);
                }

                var price = string.Empty;
                if (productsPrices?.FirstOrDefault(x => x.ProductGuid == product.Guid)?.PriceInclTax != null)
                {
                    price = new Money(productsPrices.First(x => x.ProductGuid == product.Guid).PriceInclTax, currencyIsoCode).ToString();
                }

                var fullTestSearchResultModel = new FullTextSearchResultDTO()
                {
                    ThumbnailImageUrl = product.ThumbnailImageUrl,
                    Name  = product.Name,
                    Url   = detailsPageUrl,
                    Price = new Money(productsPrices.First(x => x.ProductGuid == product.Guid).PriceInclTax, currencyIsoCode).ToString(),
                };

                fullTextSearchResultModels.Add(fullTestSearchResultModel);
            }

            return(fullTextSearchResultModels);
        }