/// <summary> /// Resolves the product item. /// </summary> /// <param name="arguments">The arguments.</param> /// <returns>The product item.</returns> public override Item ResolveProductItem(params string[] arguments) { Assert.ArgumentNotNull(arguments, "arguments"); string code = arguments[0]; Assert.IsNotNullOrEmpty(code, "code"); Item productFolderItem = Sitecore.Context.Database.GetItem(Context.Entity.GetConfiguration <BusinessCatalogSettings>().ProductsLink); Assert.IsNotNull(productFolderItem, "Products root item cannot be null."); Query query = new Query { SearchRoot = productFolderItem.ID.ToString() }; query.AppendField("Product Code", code, MatchVariant.Exactly); foreach (Item item in this.SearchProvider.Search(query, Sitecore.Context.Database)) { if (ProductRepositoryUtil.IsBasedOnTemplate(item.Template, new ID(this.ProductTemplateId))) { return(item); } } return(default(Item)); }
protected virtual T GetProduct <T>([NotNull] Item item) { Debug.ArgumentNotNull(item, "item"); T repositoryItem; using (new SiteIndependentDatabaseSwitcher(item.Database)) { if (typeof(T) == typeof(ProductBaseData) || typeof(T).IsSubclassOf(typeof(ProductBaseData))) { ID productBaseTemplateId = new ID(Configuration.Settings.GetSetting("Ecommerce.Product.BaseTemplateId")); bool basedOnProductTemplate = ProductRepositoryUtil.IsBasedOnTemplate(item.Template, productBaseTemplateId); if (!basedOnProductTemplate) { return(default(T)); } Assert.IsNotNull(this.ProductFactory, "Unable to get the product. ProductFactory cannot be null."); repositoryItem = (T)((object)this.ProductFactory.Create(item.TemplateID.ToString())); } else { repositoryItem = Context.Entity.Resolve <T>(); } if (repositoryItem is IEntity) { this.ItemToEntityMapper.Map(item, (IEntity)repositoryItem); } } return(repositoryItem); }
/// <summary> /// Gets the sub items. /// </summary> /// <typeparam name="TChild">The type of the child.</typeparam> /// <typeparam name="T">The type of the product repository item.</typeparam> /// <param name="repositoryItemCode">The repository item code.</param> /// <param name="deep">if set to <c>true</c> [deep].</param> /// <returns>The sub items.</returns> public virtual IEnumerable <TChild> GetSubItems <TChild, T>(string repositoryItemCode, bool deep) where T : IProductRepositoryItem where TChild : IProductRepositoryItem { Assert.ArgumentNotNullOrEmpty(repositoryItemCode, "repositoryItemCode"); Item parentItem = this.GetItem <T>(repositoryItemCode); Assert.IsNotNull(parentItem, "Parent item is null"); IEnumerable <Item> items = deep ? parentItem.Axes.GetDescendants() : parentItem.Children.ToArray(); if (items == null) { yield break; } foreach (Item item in items) { if (this.searchProvider is LuceneSearchProvider || ProductRepositoryUtil.IsBasedOnTemplate(item.Template, this.GetRepositoryItemTemplateId(typeof(TChild)))) { yield return(this.DataMapper.GetEntity <TChild>(item)); } } }
/// <summary> /// Gets the specified search query. /// </summary> /// <typeparam name="T">The type of product repository item.</typeparam> /// <typeparam name="TQuery">The type of the query.</typeparam> /// <param name="searchQuery">The search query.</param> /// <param name="startIndex">The start index.</param> /// <param name="pageSize">Size of the page.</param> /// <returns>The result.</returns> public virtual IEnumerable <T> Get <T, TQuery>(TQuery searchQuery, int startIndex, int pageSize) where T : IProductRepositoryItem { Assert.ArgumentNotNull(searchQuery, "searchQuery"); Assert.IsTrue(searchQuery is Query, "Query type is invalid"); Query query = searchQuery as Query; IEnumerable <Item> items = this.Get(query, typeof(T)); if (items == null) { yield break; } foreach (Item item in items.Skip(startIndex * pageSize).Take(pageSize)) { if (item != null) { if (this.searchProvider is LuceneSearchProvider || ProductRepositoryUtil.IsBasedOnTemplate(item.Template, this.GetRepositoryItemTemplateId(typeof(T)))) { T productItem = this.GetProduct <T>(item); if (productItem != null) { yield return(productItem); } } } } }
/// <summary> /// Gets the virtual product URL with analitics query string. /// </summary> /// <param name="folderNi">The folder ni.</param> /// <param name="productNi">The product ni.</param> /// <returns>The virtual product URL with analitics query string.</returns> public virtual string GetVirtualProductUrlWithAnaliticsQueryString(XPathNodeIterator folderNi, XPathNodeIterator productNi) { Assert.ArgumentNotNull(folderNi, "folderNi"); Assert.ArgumentNotNull(productNi, "productNi"); Item catalogItem = this.GetItem(folderNi); Item productItem = this.GetItem(productNi); if (catalogItem == null || productItem == null) { Log.Warn("Product catalog item and product item are null", this); return(string.Empty); } string virtualProductUrl = Context.Entity.Resolve <VirtualProductResolver>().GetVirtualProductUrl(catalogItem, productItem); if (string.IsNullOrEmpty(virtualProductUrl)) { Log.Warn("Fail to get virtual product URL", this); return(string.Empty); } string name = ProductRepositoryUtil.IsBasedOnTemplate(catalogItem.Template, new ID(Configuration.Settings.GetSetting("Ecommerce.Product.BaseTemplateId"))) ? catalogItem["Title"] : catalogItem.Name; return(this.navigationEvents.AddFollowListToQueryString(virtualProductUrl, name)); }
public override Item ResolveProductItem(params string[] arguments) { Assert.ArgumentNotNull(arguments, "arguments"); Assert.IsNotNull(ShopContext, "ShopContext"); var settings = ShopContext.GetBusinessSettings(); string name = arguments[0]; Assert.IsNotNullOrEmpty(name, "name"); name = Sitecore.MainUtil.DecodeName(name); Item item = Sitecore.Context.Database.GetItem(settings.ProductsLink); Assert.IsNotNull(item, "Products root item cannot be null."); Query query = new Query { SearchRoot = item.ID.ToString() }; query.AppendField("_name", name, MatchVariant.Exactly); Item result; foreach (Item current in this.SearchProvider.Search(query, Sitecore.Context.Database)) { if (ProductRepositoryUtil.IsBasedOnTemplate(current.Template, new ID(this.ProductTemplateId))) { result = current; return(result); } } return(null); }
/// <summary> /// Gets the price matrix price. /// </summary> /// <typeparam name="TProduct">The type of the product.</typeparam> /// <param name="product">The product.</param> /// <param name="priceMatrixName">Name of the price matrix.</param> /// <returns>The price matrix price.</returns> protected virtual decimal GetPriceMatrixPrice <TProduct>(TProduct product, string priceMatrixName) where TProduct : ProductBaseData { Assert.ArgumentNotNull(product, "product"); string field = "Price"; Item productItem = ProductRepositoryUtil.GetRepositoryItem(product, this.Database); if (productItem == null) { return(decimal.Zero); } PriceField priceField; try { priceField = productItem.Fields[field]; } catch (Exception exception) { Log.Error(exception.Message, exception, this); return(decimal.Zero); } if (priceField == null) { return(decimal.Zero); } string sitename = "Shop"; string query = string.Format("./{0}/{1}", sitename, priceMatrixName); IPriceMatrixItem priceMatrixItem = priceField.PriceMatrix.SelectSingleItem(query); CategoryItem categoryItem = priceMatrixItem as CategoryItem; if (categoryItem == null) { return(decimal.Zero); } string price = categoryItem.Amount; decimal priceValue; return(!decimal.TryParse(price, NumberStyles.Float, CultureInfo.InvariantCulture, out priceValue) ? 0 : priceValue); }
/// <summary> /// Gets the virtual product URL. /// </summary> /// <param name="catalogItem">The catalog item.</param> /// <param name="productItem">The product item.</param> /// <returns>The virtual product URL.</returns> public virtual string GetVirtualProductUrl(Item catalogItem, Item productItem) { Assert.ArgumentNotNull(catalogItem, "catalogItem"); Assert.ArgumentNotNull(productItem, "productItem"); if (catalogItem.Template != null && ProductRepositoryUtil.IsBasedOnTemplate(catalogItem.Template, this.productBaseTemplateId)) { if (this.ProductCatalogItem != default(Item) && !ProductRepositoryUtil.IsBasedOnTemplate(this.ProductCatalogItem.Template, this.productBaseTemplateId)) { catalogItem = this.ProductCatalogItem; } else { lock (this.ProductsUrlsCollection.SyncRoot) { foreach (DictionaryEntry dictionaryEntry in this.ProductsUrlsCollection) { var uriLine = (ProductUriLine)dictionaryEntry.Value; if (string.Compare(uriLine.ProductItemUri, catalogItem.Uri.ToString(), true) == 0) { catalogItem = Database.GetItem(new ItemUri(uriLine.ProductCatalogItemUri)); } } } } } string key = this.GetDisplayProductsModeKey(catalogItem); // NOTE: Check whether items are presented in the url collection lock (this.ProductsUrlsCollection.SyncRoot) { foreach (DictionaryEntry entry in this.ProductsUrlsCollection) { ProductUriLine line = (ProductUriLine)entry.Value; if (line.ProductItemUri == productItem.Uri.ToString() && line.ProductCatalogItemUri == catalogItem.Uri.ToString() && line.DisplayProductsMode == key) { return(System.Web.HttpUtility.UrlPathEncode(entry.Key as string)); } } } ProductUrlProcessor productUrlBuilder; if (Context.Entity.HasRegistration(typeof(ProductUrlProcessor), key)) { productUrlBuilder = Context.Entity.Resolve <ProductUrlProcessor>(key); } else { Log.Warn(string.Format("ProductUrlProcessor is not defined with the name: {0}. Check Unity.config file.", key), this); return(LinkManager.GetItemUrl(productItem)); } string url = productUrlBuilder.GetProductUrl(catalogItem, productItem).ToString(); Item productPresentationItem = this.GetProductPresentationItem(catalogItem); Assert.IsNotNull(productPresentationItem, "Product presentation item is null"); ProductUriLine productUriLine = new ProductUriLine { ProductItemUri = productItem.Uri.ToString(), ProductCatalogItemUri = catalogItem.Uri.ToString(), ProductPresentationItemUri = productPresentationItem.Uri.ToString(), DisplayProductsMode = key, }; string result = System.Web.HttpUtility.UrlDecode(url); lock (this.ProductsUrlsCollection.SyncRoot) { this.ProductsUrlsCollection[result] = productUriLine; } return(System.Web.HttpUtility.UrlPathEncode(url)); }