コード例 #1
0
        public IEnumerable <RelatedProductJsonResult> GetRelatedProducts(string productID)
        {
            List <RelatedProductJsonResult> relatedProducts = new List <RelatedProductJsonResult>();
            //Dictionary<string, List<ProductEntity>> productList = new RelatedProducts(this.ModelProvider, this.StorefrontContext).GetRelatedProductsLists(Sitecore.Context.Item, "RelatedProduct", 10, 0);

            CommerceStorefront currentStorefront = this.StorefrontContext.CurrentStorefront;
            string             catalog           = currentStorefront.Catalog;
            Item product = this.SearchManager.GetProduct(productID, catalog);

            var relatedProductsListFld = product.Fields["RelatedProduct"];

            if (relatedProductsListFld != null)
            {
                string        productsFldVal = relatedProductsListFld.Value;
                List <string> productList;
                if (productsFldVal == null)
                {
                    productList = (List <string>)null;
                }
                else
                {
                    productList = ((IEnumerable <string>)productsFldVal.Split('|')).ToList <string>();
                }
                List <string> source = productList;
                if (source != null && source.Any <string>())
                {
                    source.Remove("");
                    foreach (string id in source)
                    {
                        Item relatedProduct = Context.Database.GetItem(new ID(id));

                        RelatedProductJsonResult result = new RelatedProductJsonResult();
                        result.ProductName = relatedProduct.DisplayName;
                        MultilistField imagesFld = (MultilistField)relatedProduct.Fields["Images"];
                        if (imagesFld != null && !String.IsNullOrEmpty(imagesFld.Value))
                        {
                            MediaItem imageItem = Sitecore.Context.Database.GetItem(imagesFld.TargetIDs[0]);
                            result.Image = imageItem != null?MediaManager.GetMediaUrl(imageItem) : " ";
                        }
                        result.Description = relatedProduct["Description"];
                        result.Quantity    = 1;

                        result.ProductUrl = id.Equals(currentStorefront.GiftCardProductId, StringComparison.OrdinalIgnoreCase) ? currentStorefront.GiftCardPageLink : LinkManager.GetDynamicUrl(relatedProduct);

                        relatedProducts.Add(result);
                    }
                }
            }
            return(relatedProducts);
        }
コード例 #2
0
        public IEnumerable <RelatedProductJsonResult> GetRelatedProducts(string productId)
        {
            List <RelatedProductJsonResult> relatedProducts = new List <RelatedProductJsonResult>();
            CommerceStorefront currentStorefront            = this.StorefrontContext.CurrentStorefront;
            string             catalog = currentStorefront.Catalog;
            Item product = this.SearchManager.GetProduct(productId, catalog);

            var relatedProductsListFld = product.Fields["RelatedProduct"];

            if (relatedProductsListFld != null)
            {
                string        productsFldVal = relatedProductsListFld.Value;
                List <string> productList;
                if (productsFldVal == null)
                {
                    productList = (List <string>)null;
                }
                else
                {
                    productList = ((IEnumerable <string>)productsFldVal.Split('|')).ToList <string>();
                }
                List <string> source = productList;
                if (source != null && source.Any <string>())
                {
                    source.Remove("");
                    foreach (string id in source)
                    {
                        Item cxtRelatedProduct = Context.Database.GetItem(new ID(id));
                        Assert.IsNotNull((object)cxtRelatedProduct, string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Unable to locate the product with id: {0}", (object)id));
                        var  relProductId   = cxtRelatedProduct["ProductId"];
                        Item relatedProduct = this.SearchManager.GetProduct(relProductId, catalog);

                        RelatedProductJsonResult result = new RelatedProductJsonResult();


                        result.ProductId   = relProductId;
                        result.ProductName = relatedProduct.DisplayName;
                        MultilistField imagesFld = (MultilistField)relatedProduct.Fields["Images"];
                        if (imagesFld != null && !String.IsNullOrEmpty(imagesFld.Value))
                        {
                            MediaItem imageItem = Sitecore.Context.Database.GetItem(imagesFld.TargetIDs[0]);
                            result.Image = imageItem != null?MediaManager.GetMediaUrl(imageItem) : " ";
                        }
                        result.Description = relatedProduct["Description"];
                        result.Quantity    = 1;

                        result.ProductUrl = id.Equals(currentStorefront.GiftCardProductId, StringComparison.OrdinalIgnoreCase) ? currentStorefront.GiftCardPageLink : LinkManager.GetDynamicUrl(relatedProduct);

                        string str1 = relatedProduct[Sitecore.Commerce.XA.Foundation.Common.Constants.ItemFieldNames.VariationProperties];

                        if (!string.IsNullOrWhiteSpace(str1))
                        {
                            string str2    = str1;
                            char[] chArray = new char[1] {
                                '|'
                            };
                            foreach (string labelKey in str2.Split(chArray))
                            {
                                VariantDefinitionEntity model = this.ModelProvider.GetModel <VariantDefinitionEntity>();
                                model.PropertyName = labelKey;
                                model.DisplayName  = this.StorefrontContext.GetVariantSpecificationLabels(labelKey, true);
                                result.VariantDefinitionList.Add(model);
                            }
                        }
                        if (relatedProduct.HasChildren)
                        {
                            if (result.VariantDefinitionList.Count() > 0)
                            {
                                foreach (var definition in result.VariantDefinitionList)
                                {
                                    List <RelatedProductVariantJsonResult> options = result.GetDistinctVariantPropertyValues(relatedProduct.Children.ToList(), definition.PropertyName);
                                    if (options.Count() > 0)
                                    {
                                        result.VariantOptions.Add(new VariantOptionJsonResult
                                        {
                                            Label   = definition.DisplayName,
                                            Options = options
                                        });
                                    }
                                }
                            }
                        }
                        relatedProducts.Add(result);
                    }
                }
            }
            return(relatedProducts);
        }