public override bool IsApplicableToEntity(CommerceEntity entity, string itemId) { return(entity.GetType() == EntityType); }
/// <summary> /// Retrieves all component types applicable for the sellable item /// </summary> /// <param name="commerceEntity">Sellable item for which to get the applicable components</param> /// <param name="itemId"></param> /// <param name="context"></param> /// <returns></returns> public List <Type> GetApplicableComponentTypes(CommerceEntity commerceEntity, string itemId, CommerceContext context) { // Get the item definition var catalogs = commerceEntity.GetComponent <CatalogsComponent>(); // TODO: What happens if a sellableitem is part of multiple catalogs? var catalog = catalogs.GetComponent <CatalogComponent>(); var itemDefinition = catalog.ItemDefinition; var allComponentTypes = GetAllComponentTypes(); var applicableComponentTypes = new List <Type>(); foreach (var componentType in allComponentTypes) { System.Attribute[] attrs = System.Attribute.GetCustomAttributes(componentType); if (attrs.Any(attr => attr is AddToSellableItemAttribute) && commerceEntity is SellableItem) { var sellableItemsAttribute = attrs.Single(attr => attr is SellableItemAttributeBase) as SellableItemAttributeBase; var addToSellableItem = sellableItemsAttribute.AddToSellableItem; if (IsApplicableComponent(itemId, addToSellableItem)) { applicableComponentTypes.Add(componentType); } } else if (attrs.Any(attr => attr is AddToItemDefinitionAttribute && ((AddToItemDefinitionAttribute)attr).ItemDefinition == itemDefinition)) { var sellableItemsAttribute = attrs.Single(attr => attr is AddToItemDefinitionAttribute) as AddToItemDefinitionAttribute; var addToSellableItem = sellableItemsAttribute.AddToSellableItem; if (IsApplicableComponent(itemId, addToSellableItem)) { applicableComponentTypes.Add(componentType); } } else if (attrs.Any(attr => attr is AddToEntityTypeAttribute && ((AddToEntityTypeAttribute)attr).EntityType == commerceEntity.GetType())) { applicableComponentTypes.Add(componentType); } else if (attrs.Any(attr => attr is AddToAllEntityTypesAttribute)) { applicableComponentTypes.Add(componentType); } } return(applicableComponentTypes); }