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); }
/// <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); }
/// <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); }