getSortedAttributes function

List<Attribute> getSortedAttributes(
  1. Product product,
  2. List<String> attributeGroupOrder,
  3. Set<String> attributesToExcludeIfStatusIsUnknown,
  4. ProductPreferences preferences,
  5. String importance, {
  6. bool excludeMainScoreAttributes = true,
})

Returns the attributes, ordered by importance desc and attribute group order

Implementation

List<Attribute> getSortedAttributes(
  Product product,
  List<String> attributeGroupOrder,
  Set<String> attributesToExcludeIfStatusIsUnknown,
  ProductPreferences preferences,
  String importance, {
  bool excludeMainScoreAttributes = true,
}) {
  final List<Attribute> result = <Attribute>[];
  if (product.attributeGroups == null) {
    return result;
  }
  final Map<String, List<Attribute>> mandatoryAttributesByGroup =
      <String, List<Attribute>>{};
  // collecting all the mandatory attributes, by group
  for (final AttributeGroup attributeGroup in product.attributeGroups!) {
    mandatoryAttributesByGroup[attributeGroup.id!] = getFilteredAttributes(
      attributeGroup,
      importance,
      attributesToExcludeIfStatusIsUnknown,
      preferences,
      excludeMainScoreAttributes: excludeMainScoreAttributes,
    );
  }

  // now ordering by attribute group order
  for (final String attributeGroupId in attributeGroupOrder) {
    final List<Attribute>? attributes =
        mandatoryAttributesByGroup[attributeGroupId];
    if (attributes != null) {
      result.addAll(attributes);
    }
  }
  return result;
}