From 557520148e0e3906e2e24579579facff76339490 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Mon, 15 Apr 2019 23:19:44 +0530 Subject: [PATCH] fix: Item Variant Attribute Order (#17237) - Show Item Variant Attributes in order they appear in child table --- .../product_configurator/item_variants_cache.py | 13 +++++++++++++ erpnext/portal/product_configurator/utils.py | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/erpnext/portal/product_configurator/item_variants_cache.py b/erpnext/portal/product_configurator/item_variants_cache.py index cd557b5075..458c229e2f 100644 --- a/erpnext/portal/product_configurator/item_variants_cache.py +++ b/erpnext/portal/product_configurator/item_variants_cache.py @@ -39,6 +39,19 @@ class ItemVariantsCacheManager: return frappe.cache().hget('optional_attributes', self.item_code) + def get_ordered_attribute_values(self): + val = frappe.cache().get_value('ordered_attribute_values_map') + if val: return val + + all_attribute_values = frappe.db.get_all('Item Attribute Value', + ['attribute_value', 'idx', 'parent'], order_by='idx asc') + + ordered_attribute_values_map = frappe._dict({}) + for d in all_attribute_values: + ordered_attribute_values_map.setdefault(d.parent, []).append(d.attribute_value) + + frappe.cache().set_value('ordered_attribute_values_map', ordered_attribute_values_map) + return ordered_attribute_values_map def build_cache(self): parent_item_code = self.item_code diff --git a/erpnext/portal/product_configurator/utils.py b/erpnext/portal/product_configurator/utils.py index 3594bc4467..f02212c6a6 100644 --- a/erpnext/portal/product_configurator/utils.py +++ b/erpnext/portal/product_configurator/utils.py @@ -167,8 +167,13 @@ def get_attributes_and_values(item_code): if attribute in attribute_list: valid_options.setdefault(attribute, set()).add(attribute_value) + # build attribute values in idx order + ordered_attribute_value_map = item_cache.get_ordered_attribute_values() for attr in attributes: - attr['values'] = valid_options.get(attr.attribute, []) + valid_attribute_values = valid_options.get(attr.attribute, []) + ordered_values = ordered_attribute_value_map.get(attr.attribute, []) + attr['values'] = [v for v in ordered_values if v in valid_attribute_values] + attr['values'] = valid_attribute_values return attributes