public RelatedCatalogItemsViewModel GetRelationshipsFromItem(Item catalogItem, Rendering rendering) { if (catalogItem == null || !catalogItem.IsDerived(Foundation.Commerce.Templates.Commerce.CatalogItem.Id) || !catalogItem.FieldHasValue(Foundation.Commerce.Templates.Commerce.CatalogItem.Fields.RelationshipList)) { return(null); } RelationshipField field = catalogItem.Fields[Foundation.Commerce.Templates.Commerce.CatalogItem.Fields.RelationshipList]; var model = new RelatedCatalogItemsViewModel(); var productRelationshipInfoList = field.GetRelationships(); productRelationshipInfoList = productRelationshipInfoList.OrderBy(x => x.Rank); var productModelList = GroupRelationshipsByDescription(productRelationshipInfoList); model.RelatedProducts.AddRange(productModelList); return(model); }
/// <summary> /// Gets a lists of target items from a relationship field /// </summary> /// <param name="storefront">The storefront.</param> /// <param name="visitorContext">The visitor context.</param> /// <param name="field">the relationship field</param> /// <param name="rendering">The target renering.</param> /// <param name="relationshipNames">the names of the relationships, to retrieve (for example upsell).</param> /// <returns> /// a list of relationship targets or null if no items found /// </returns> public RelatedCatalogItemsViewModel GetRelationshipsFromField([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, RelationshipField field, Rendering rendering, IEnumerable <string> relationshipNames) { Assert.ArgumentNotNull(storefront, "storefront"); relationshipNames = relationshipNames ?? Enumerable.Empty <string>(); relationshipNames = relationshipNames.Select(s => s.Trim()); var model = new RelatedCatalogItemsViewModel(); if (field != null) { var productRelationshipInfoList = field.GetRelationships(); productRelationshipInfoList = productRelationshipInfoList.OrderBy(x => x.Rank); var productModelList = this.GroupRelationshipsByDescription(storefront, visitorContext, field, relationshipNames, productRelationshipInfoList, rendering); model.RelatedProducts.AddRange(productModelList); } model.Initialize(rendering); return(model); }