Exemplo n.º 1
0
        public ProductViewModel GetWildCardProductViewModel(Item datasource, Rendering currentRendering)
        {
            ProductViewModel productViewModel;
            var productId = CatalogUrlManager.ExtractItemIdFromCurrentUrl().Replace(" ", "-");
            var virtualProductCacheKey = string.Format(CultureInfo.InvariantCulture, "VirtualProduct_{0}", productId);

            if (this.CurrentSiteContext.Items.Contains(virtualProductCacheKey))
            {
                productViewModel = this.CurrentSiteContext.Items[virtualProductCacheKey] as ProductViewModel;
            }
            else
            {
                if (string.IsNullOrEmpty(productId))
                {
                    //No ProductId passed in on the URL
                    //Use to Storefront DefaultProductId
                    productId = StorefrontManager.CurrentStorefront.DefaultProductId;
                }

                var productItem = SearchNavigation.GetProduct(productId, this.CurrentCatalog.Name);

                if (productItem == null)
                {
                    var message = string.Format(CultureInfo.InvariantCulture, "The requested product '{0}' does not exist in the catalog '{1}' or cannot be displayed in the language '{2}'", productId, this.CurrentCatalog.Name, Context.Language);
                    Log.Error(message, this);
                    throw new InvalidOperationException(message);
                }

                datasource       = productItem;
                productViewModel = this.GetProductViewModel(datasource, currentRendering);
                this.CurrentSiteContext.Items.Add(virtualProductCacheKey, productViewModel);
            }

            return(productViewModel);
        }
        private string GetCatalogItemIdFromUrl(bool isProduct)
        {
            var catalogItemId = string.Empty;
            var rawUrl        = HttpContext.Current.Request.RawUrl;
            var urlTokens     = rawUrl.Split('/');

            if (urlTokens.Any())
            {
                var item = urlTokens.Last();
                var queryStringPosition = item.IndexOf("?", StringComparison.OrdinalIgnoreCase);
                if (queryStringPosition > 0)
                {
                    item = item.Substring(0, queryStringPosition);
                }

                if (isProduct && urlTokens.Length >= 4)
                {
                    var parentCategoryName = urlTokens[urlTokens.Length - 2];
                    item = $"{parentCategoryName}{item}";
                }
                catalogItemId = CatalogUrlManager.ExtractItemId(item);
            }

            return(catalogItemId);
        }
        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);
        }
Exemplo n.º 4
0
        public ProductViewModel GetGiftCardViewModel(Item productItem, Rendering currentRendering)
        {
            if (this.CurrentSiteContext.Items[CurrentProductViewModelKeyName] != null)
            {
                return((ProductViewModel)this.CurrentSiteContext.Items[CurrentProductViewModelKeyName]);
            }

            var variants = new List <VariantViewModel>();

            if (productItem != null && productItem.HasChildren)
            {
                foreach (Item item in productItem.Children)
                {
                    var v = new VariantViewModel(item);
                    variants.Add(v);
                }
            }

            var productViewModel = new ProductViewModel(productItem);

            productViewModel.Initialize(currentRendering, variants);
            productViewModel.ProductName = productViewModel.DisplayName;

            if (this.CurrentSiteContext.UrlContainsCategory)
            {
                productViewModel.ParentCategoryId = CatalogUrlManager.ExtractCategoryNameFromCurrentUrl();

                var category = this.CatalogManager.GetCategory(productViewModel.ParentCategoryId);
                if (category != null)
                {
                    productViewModel.ParentCategoryName = category.DisplayName;
                }
            }

            //Special handling for gift card
            if (productViewModel.ProductId == StorefrontManager.CurrentStorefront.GiftCardProductId)
            {
                productViewModel.GiftCardAmountOptions = GetGiftCardAmountOptions(productViewModel);
            }
            else
            {
                this.CatalogManager.GetProductPrice(this.CurrentVisitorContext, productViewModel);
                productViewModel.CustomerAverageRating = this.CatalogManager.GetProductRating(productItem);
            }

            this.CurrentSiteContext.Items[CurrentProductViewModelKeyName] = productViewModel;

            return(productViewModel);
        }
        /// <summary>
        /// This method returns the dynamicly generated URL based on the item type.
        /// </summary>
        /// <param name="item">The item</param>
        /// <param name="options">The options</param>
        /// <returns>The dynamically built URL</returns>
        public override string GetDynamicUrl(Item item, LinkUrlOptions options)
        {
            Assert.ArgumentNotNull(item, "item");
            Assert.ArgumentNotNull(options, "options");

            var url      = string.Empty;
            var itemType = item.ItemType();

            bool productCatalogLinkRequired = Sitecore.Web.WebUtil.GetRawUrl().IndexOf(ProductItemResolver.NavigationItemName, System.StringComparison.OrdinalIgnoreCase) >= 0;

            if (productCatalogLinkRequired)
            {
                url = CatalogUrlManager.BuildProductCatalogLink(item);
            }
            else if (this.UseShopLinks)
            {
                if (itemType == StorefrontConstants.ItemTypes.Product)
                {
                    url = CatalogUrlManager.BuildProductShopLink(item, this.IncludeCatalog, this.IncludeFriendlyName, true);
                }
                else if (itemType == StorefrontConstants.ItemTypes.Category)
                {
                    url = CatalogUrlManager.BuildCategoryShopLink(item, this.IncludeCatalog, this.IncludeFriendlyName);
                }
                else if (itemType == StorefrontConstants.ItemTypes.Variant)
                {
                    url = CatalogUrlManager.BuildVariantShopLink(item, this.IncludeCatalog, this.IncludeFriendlyName, true);
                }
            }
            else
            {
                if (itemType == StorefrontConstants.ItemTypes.Product)
                {
                    url = CatalogUrlManager.BuildProductLink(item, this.IncludeCatalog, this.IncludeFriendlyName);
                }
                else if (itemType == StorefrontConstants.ItemTypes.Category)
                {
                    url = CatalogUrlManager.BuildCategoryLink(item, this.IncludeCatalog, this.IncludeFriendlyName);
                }
            }

            if (string.IsNullOrEmpty(url))
            {
                url = base.GetDynamicUrl(item, options);
            }

            return(url);
        }
        /// <summary>
        /// Visiteds the product details page.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <returns>
        /// The manager response
        /// </returns>
        public virtual ManagerResponse <CatalogResult, bool> VisitedProductDetailsPage([NotNull] CommerceStorefront storefront)
        {
            Assert.ArgumentNotNull(storefront, "storefront");

            string productId          = CatalogUrlManager.ExtractItemIdFromCurrentUrl();
            string parentCategoryName = CatalogUrlManager.ExtractCategoryNameFromCurrentUrl();
            var    request            = new VisitedProductDetailsPageRequest(storefront.ShopName, productId, productId, parentCategoryName, parentCategoryName);

            var result = this.CatalogServiceProvider.VisitedProductDetailsPage(request);

            if (!result.Success)
            {
                Helpers.LogSystemMessages(result.SystemMessages, result);
            }

            return(new ManagerResponse <CatalogResult, bool>(result, result.Success));
        }
        /// <summary>
        /// This method returns the dynamicly generated URL based on the item type.
        /// </summary>
        /// <param name="item">The item</param>
        /// <param name="options">The options</param>
        /// <returns>The dynamically built URL</returns>
        public override string GetDynamicUrl(Item item, LinkUrlOptions options)
        {
            Assert.ArgumentNotNull(item, "item");
            Assert.ArgumentNotNull(options, "options");

            var url           = string.Empty;
            var searchManager = CommerceTypeLoader.CreateInstance <ICommerceSearchManager>();

            if (this.UseShopLinks)
            {
                if (searchManager.IsItemProduct(item))
                {
                    url = CatalogUrlManager.BuildProductShopLink(item, this.IncludeCatalog, this.IncludeFriendlyName, true);
                }
                else if (searchManager.IsItemCategory(item))
                {
                    url = CatalogUrlManager.BuildCategoryShopLink(item, this.IncludeCatalog, this.IncludeFriendlyName);
                }
                else if (this.UseShopLinks && searchManager.IsItemVariant(item))
                {
                    url = CatalogUrlManager.BuildVariantShopLink(item, this.IncludeCatalog, this.IncludeFriendlyName, true);
                }
            }
            else
            {
                if (searchManager.IsItemProduct(item))
                {
                    url = CatalogUrlManager.BuildProductLink(item, this.IncludeCatalog, this.IncludeFriendlyName);
                }
                else if (searchManager.IsItemCategory(item))
                {
                    url = CatalogUrlManager.BuildCategoryLink(item, this.IncludeCatalog, this.IncludeFriendlyName);
                }
            }

            if (string.IsNullOrEmpty(url))
            {
                url = base.GetDynamicUrl(item, options);
            }

            return(url);
        }
        /// <summary>
        /// This method returns the current category by URL
        /// </summary>
        /// <returns>The category.</returns>
        public Category GetCurrentCategoryByUrl()
        {
            Category currentCategory;

            var categoryId = CatalogUrlManager.ExtractItemIdFromCurrentUrl();

            string virtualCategoryCacheKey = string.Format(CultureInfo.InvariantCulture, "VirtualCategory_{0}", categoryId);

            if (CurrentSiteContext.Items.Contains(virtualCategoryCacheKey))
            {
                currentCategory = CurrentSiteContext.Items[virtualCategoryCacheKey] as Category;
            }
            else
            {
                currentCategory = GetCategory(categoryId);
                CurrentSiteContext.Items.Add(virtualCategoryCacheKey, currentCategory);
            }

            return(currentCategory);
        }
Exemplo n.º 9
0
        private ProductViewModel GetProductViewModel(Item productItem, Rendering rendering)
        {
            if (this.CurrentSiteContext.Items[CurrentProductViewModelKeyName] != null)
            {
                return((ProductViewModel)this.CurrentSiteContext.Items[CurrentProductViewModelKeyName]);
            }

            var variants = new List <VariantViewModel>();

            if (productItem != null && productItem.HasChildren)
            {
                foreach (Item item in productItem.Children)
                {
                    var v = new VariantViewModel(item);
                    variants.Add(v);
                }
            }

            var productViewModel = new ProductViewModel(productItem);

            productViewModel.Initialize(rendering, variants);
            productViewModel.ProductName = productViewModel.DisplayName;

            if (this.CurrentSiteContext.UrlContainsCategory)
            {
                productViewModel.ParentCategoryId = CatalogUrlManager.ExtractCategoryNameFromCurrentUrl();
                var category = _catalogManager.GetCategory(productViewModel.ParentCategoryId);

                if (category != null)
                {
                    productViewModel.ParentCategoryName = category.DisplayName;
                }
            }

            _catalogManager.GetProductPrice(this.CurrentVisitorContext, productViewModel);
            productViewModel.CustomerAverageRating = _catalogManager.GetProductRating(productItem);
            this.CurrentSiteContext.Items[CurrentProductViewModelKeyName] = productViewModel;

            return(productViewModel);
        }
Exemplo n.º 10
0
        /// <summary>
        /// This method returns the Route data value
        /// </summary>
        /// <param name="routeData">The route</param>
        /// <returns>The value if found, empty string if route is found but not value, null if route invalid</returns>
        public virtual CatalogRouteData GetRouteDataValue(RouteData routeData)
        {
            var data = new CatalogRouteData();

            if (routeData.Values.ContainsKey(ItemTypeField))
            {
                data.IsProduct = routeData.Values[ItemTypeField].ToString() == ProductItemType ? true : false;
            }
            else
            {
                return(null);
            }

            if (routeData.Values.ContainsKey(IdField))
            {
                data.Id = CatalogUrlManager.ExtractItemId(routeData.Values[IdField].ToString());
            }
            else
            {
                return(null);
            }

            if (routeData.Values.ContainsKey(CatalogField))
            {
                data.Catalog = routeData.Values[CatalogField].ToString();
            }

            if (string.IsNullOrEmpty(data.Catalog))
            {
                var defaultCatalog = StorefrontManager.CurrentStorefront.DefaultCatalog;

                if (defaultCatalog != null)
                {
                    data.Catalog = defaultCatalog.Name;
                }
            }

            return(data);
        }
Exemplo n.º 11
0
        /// <summary>
        /// This method returns the Route data value
        /// </summary>
        /// <param name="routeData">The route</param>
        /// <returns>The value if found, empty string if route is found but not value, null if route invalid</returns>
        public virtual CatalogRouteData GetRouteDataValue(RouteData routeData)
        {
            var data = new CatalogRouteData();

            if (routeData.Values.ContainsKey(ItemTypeField))
            {
                if (routeData.Values[ItemTypeField].ToString() == CatalogItemType)
                {
                    var  currentStorefront  = StorefrontManager.CurrentStorefront;
                    Item productCatalogItem = currentStorefront.HomeItem.Axes.GetDescendant(NavigationItemName + "/" + routeData.Values["pathElements"].ToString());
                    if (productCatalogItem != null)
                    {
                        data.IsProduct = (productCatalogItem.ItemType() == StorefrontConstants.ItemTypes.Product);
                        data.Id        = productCatalogItem.Name;
                    }
                }
                else
                {
                    data.IsProduct = routeData.Values[ItemTypeField].ToString() == ProductItemType ? true : false;
                }
            }
            else
            {
                return(null);
            }

            if (routeData.Values.ContainsKey(IdField))
            {
                data.Id = CatalogUrlManager.ExtractItemId(routeData.Values[IdField].ToString());
            }
            else
            {
                if (string.IsNullOrWhiteSpace(data.Id))
                {
                    return(null);
                }
            }

            if (routeData.Values.ContainsKey(CatalogField))
            {
                data.Catalog = routeData.Values[CatalogField].ToString();
            }

            if (string.IsNullOrEmpty(data.Catalog))
            {
                var defaultCatalog = StorefrontManager.CurrentStorefront.DefaultCatalog;

                if (defaultCatalog != null)
                {
                    data.Catalog = defaultCatalog.Name;
                }
            }

            if (routeData.Values.ContainsKey(CategoryField))
            {
                var siteContext = CommerceTypeLoader.CreateInstance <ISiteContext>();

                siteContext.UrlContainsCategory = true;
            }

            return(data);
        }