protected virtual CatalogItemVariantsRenderingModel GetCatalogItemVariantsRenderingModel(
            IVisitorContext visitorContext,
            Item productItem)
        {
            Assert.ArgumentNotNull(visitorContext, nameof(visitorContext));
            //Important: make sure to use the CurrentCatalogItemVariantsRenderingModelKeyName constant here
            if (SiteContext.Items[CurrentCatalogItemVariantsRenderingModelKeyName] != null)
            {
                return((CatalogItemVariantsRenderingModel)SiteContext.Items[CurrentCatalogItemVariantsRenderingModelKeyName]);
            }
            CommerceStorefront   currentStorefront = StorefrontContext.CurrentStorefront;
            List <VariantEntity> variantEntityList = new List <VariantEntity>();

            if (productItem != null && productItem.HasChildren)
            {
                foreach (Item child in productItem.Children)
                {
                    VariantEntity model = ModelProvider.GetModel <VariantEntity>();
                    model.Initialize(child);
                    variantEntityList.Add(model);
                }
            }
            ProductEntity productEntity = ModelProvider.GetModel <ProductEntity>();

            productEntity.Initialize(currentStorefront, productItem, variantEntityList);
            CatalogItemVariantsRenderingModel catalogModel = ModelProvider.GetModel <CatalogItemVariantsRenderingModel>();

            Init(catalogModel);
            if (SiteContext.UrlContainsCategory)
            {
                catalogModel.ParentCategoryId = CatalogUrlManager.ExtractCategoryNameFromCurrentUrl();
                Item category = SearchManager.GetCategory(catalogModel.ParentCategoryId, currentStorefront.Catalog);
                if (category != null)
                {
                    catalogModel.ParentCategoryName = category.DisplayName;
                }
            }
            if (string.Equals(catalogModel.ProductId, currentStorefront.GiftCardProductId, StringComparison.Ordinal))
            {
                catalogModel.GiftCardAmountOptions = GetGiftCardAmountOptions(visitorContext, currentStorefront, productEntity);
            }
            else
            {
                CatalogManager.GetProductPrice(currentStorefront, visitorContext, productEntity, null);
                catalogModel.CustomerAverageRating = CatalogManager.GetProductRating(productItem, null);
            }
            catalogModel.Initialize(productEntity, false);
            SiteContext.Items["CurrentCatalogItemVariantsRenderingModel"] = catalogModel;
            return(catalogModel);
        }
        protected override ICollection <ProductEntity> AdjustProductPriceAndStockStatus(IVisitorContext visitorContext, SearchResults searchResult, Item currentCategory, StringPropertyCollection propertyBag = null)
        {
            Assert.ArgumentNotNull(currentCategory, nameof(currentCategory));
            Assert.ArgumentNotNull(searchResult, nameof(searchResult));
            CommerceStorefront   currentStorefront = this.StorefrontContext.CurrentStorefront;
            List <ProductEntity> productEntityList = new List <ProductEntity>();
            string str = "Category/" + currentCategory.Name;

            if (this.SiteContext.Items[str] != null)
            {
                return((ICollection <ProductEntity>) this.SiteContext.Items[str]);
            }
            if (searchResult.SearchResultItems != null && searchResult.SearchResultItems.Count > 0)
            {
                foreach (Item searchResultItem in searchResult.SearchResultItems)
                {
                    ProductEntity model = this.ModelProvider.GetModel <ProductEntity>();
                    model.Initialize(currentStorefront, searchResultItem, this.GetProductVariants(searchResultItem));
                    productEntityList.Add(model);
                }
                this.CatalogManager.GetProductBulkPrices(currentStorefront, visitorContext, productEntityList, null);
                this.InventoryManager.GetProductsStockStatus(currentStorefront, productEntityList, currentStorefront.UseIndexFileForProductStatusInLists, null);
                foreach (ProductEntity productEntity1 in productEntityList)
                {
                    ProductEntity productEntity = productEntity1;
                    Item          productItem   = searchResult.SearchResultItems.FirstOrDefault(item =>
                    {
                        if (item.Name == productEntity.ProductId)
                        {
                            return(item.Language == Context.Language);
                        }
                        return(false);
                    });
                    if (productItem != null)
                    {
                        productEntity.CustomerAverageRating = this.CatalogManager.GetProductRating(productItem, null);
                    }
                }
            }
            this.SiteContext.Items[str] = productEntityList;
            return(productEntityList);
        }
        protected virtual ProductSummaryViewModel CatalogItemRenderingModel(IVisitorContext visitorContext, Item productItem)
        {
            Assert.ArgumentNotNull(visitorContext, "visitorContext");

            var currentStorefront = StorefrontContext.CurrentStorefront;
            var variantEntityList = new List <VariantEntity>();

            if (productItem != null && productItem.HasChildren)
            {
                foreach (Item variant in productItem.Children)
                {
                    VariantEntity model = ModelProvider.GetModel <VariantEntity>();
                    model.Initialize(variant);
                    variantEntityList.Add(model);
                }
            }
            ProductEntity productEntity = ModelProvider.GetModel <ProductEntity>();

            productEntity.Initialize(currentStorefront, productItem, variantEntityList);
            var renderingModel = ModelProvider.GetModel <ProductSummaryViewModel>();

            var products = new List <ProductEntity> {
                productEntity
            };

            InventoryManager.GetProductsStockStatus(currentStorefront, products, currentStorefront.UseIndexFileForProductStatusInLists);
            CatalogManager.GetProductBulkPrices(currentStorefront, visitorContext, products);
            if (productEntity.ProductId == currentStorefront.GiftCardProductId)
            {
                renderingModel.GiftCardAmountOptions = GetGiftCardAmountOptions(visitorContext, currentStorefront, productEntity);
            }
            else
            {
                renderingModel.CustomerAverageRating = CatalogManager.GetProductRating(productItem);
            }
            renderingModel.Initialize(productEntity, false);
            return(renderingModel);
        }
        public virtual CatalogItemRenderingModel XcGetCatalogItemRenderingModel(IVisitorContext visitorContext, Item productItem)
        {
            Assert.ArgumentNotNull(visitorContext, nameof(visitorContext));
            CommerceStorefront   currentStorefront = this.StorefrontContext.CurrentStorefront;
            List <VariantEntity> variantEntityList = new List <VariantEntity>();

            if (productItem != null && productItem.HasChildren)
            {
                var childList = productItem.Children.Where(child =>
                {
                    var variantPersonalizationId = child["PersonalizationId"];
                    var liveDate   = child["LiveDate"];
                    var expiryDate = child["ExpiryDate"];

                    if (this.PersonalizationId.Equals(child["PersonalizationId"], System.StringComparison.OrdinalIgnoreCase))
                    {
                        if (!string.IsNullOrEmpty(liveDate))
                        {
                            var parseSuccess = DateTimeOffset.TryParseExact(liveDate, "yyyyMMddTHHmmss", null, DateTimeStyles.None, out var variantLiveDate);
                            if (parseSuccess && variantLiveDate > DateTimeOffset.Now)
                            {
                                return(false);
                            }
                        }

                        if (!string.IsNullOrEmpty(expiryDate))
                        {
                            var parseSuccess = DateTimeOffset.TryParseExact(expiryDate, "yyyyMMddTHHmmss", null, DateTimeStyles.None, out var variantExpiryDate);
                            if (parseSuccess && variantExpiryDate <= DateTimeOffset.Now)
                            {
                                return(false);
                            }
                        }

                        return(true);
                    }

                    return(false);
                });
                if (!childList.Any())
                {
                    childList = productItem.Children.Where(child => string.IsNullOrEmpty(child["PersonalizationId"]));
                }
                foreach (Item variantItem in childList)
                {
                    VariantEntity model = this.ModelProvider.GetModel <VariantEntity>();
                    model.Initialize(variantItem);
                    variantEntityList.Add(model);
                }
            }
            ProductEntity model1 = this.ModelProvider.GetModel <ProductEntity>();

            model1.Initialize(currentStorefront, productItem, variantEntityList);
            CatalogItemRenderingModel model2 = this.ModelProvider.GetModel <CatalogItemRenderingModel>();

            this.Init(model2);
            if (this.SiteContext.UrlContainsCategory)
            {
                model2.ParentCategoryId = this.CatalogUrlManager.ExtractCategoryNameFromCurrentUrl();
                Item category = this.SearchManager.GetCategory(model2.ParentCategoryId, currentStorefront.Catalog);
                if (category != null)
                {
                    model2.ParentCategoryName = category.DisplayName;
                }
            }
            if (model2.ProductId == currentStorefront.GiftCardProductId)
            {
                model2.GiftCardAmountOptions = this.GetGiftCardAmountOptions(visitorContext, currentStorefront, model1);
            }
            else
            {
                this.CatalogManager.GetProductPrice(currentStorefront, visitorContext, model1, null);
                model2.CustomerAverageRating = this.CatalogManager.GetProductRating(productItem, null);
            }
            model2.Initialize(model1, false);
            //this.SiteContext.Items["CurrentCatalogItemRenderingModel"] = model2;
            if (model2.CatalogItem.HasChildren)
            {
                var childList = model2.CatalogItem.Children.Where(child => this.PersonalizationId.Equals(child["PersonalizationId"], System.StringComparison.OrdinalIgnoreCase));
                if (!childList.Any())
                {
                    childList = model2.CatalogItem.Children.Where(child => string.IsNullOrEmpty(child["PersonalizationId"]));
                }
                foreach (Item child in childList)
                {
                    MultilistField field = child.Fields["Images"];
                    if (field != null)
                    {
                        model2.Images.Clear();
                        foreach (ID targetId in field.TargetIDs)
                        {
                            model2.Images.Add(child.Database.GetItem(targetId));
                        }
                        return(model2);
                    }
                }
            }

            return(model2);
        }