fix: Behaviour if Item Variants Cache generation
- Revert to old behaviour where variant doesnt need to be published
- Delete unused functions
diff --git a/erpnext/e_commerce/doctype/website_item/website_item.py b/erpnext/e_commerce/doctype/website_item/website_item.py
index 470499f..24546be 100644
--- a/erpnext/e_commerce/doctype/website_item/website_item.py
+++ b/erpnext/e_commerce/doctype/website_item/website_item.py
@@ -203,16 +203,15 @@
context.body_class = "product-page"
context.parents = get_parent_item_groups(self.item_group, from_item=True) # breadcumbs
- self.attributes = frappe.get_all("Item Variant Attribute",
+ self.attributes = frappe.get_all(
+ "Item Variant Attribute",
fields=["attribute", "attribute_value"],
- filters={"parent": self.item_code})
+ filters={"parent": self.item_code}
+ )
if self.slideshow:
context.update(get_slideshow(self))
- self.set_variant_context(context)
- self.set_attribute_context(context)
- self.set_disabled_attributes(context)
self.set_metatags(context)
self.set_shopping_cart_data(context)
@@ -237,61 +236,6 @@
return context
- def set_variant_context(self, context):
- if not self.has_variants:
- return
-
- context.no_cache = True
- variant = frappe.form_dict.variant
-
- # load variants
- # also used in set_attribute_context
- context.variants = frappe.get_all(
- "Item",
- filters={
- "variant_of": self.item_code,
- "published_in_website": 1
- },
- order_by="name asc")
-
- # the case when the item is opened for the first time from its list
- if not variant and context.variants:
- variant = context.variants[0]
-
- if variant:
- context.variant = frappe.get_doc("Item", variant)
- fields = ("website_image", "website_image_alt", "web_long_description", "description",
- "website_specifications")
-
- for fieldname in fields:
- if context.variant.get(fieldname):
- value = context.variant.get(fieldname)
- if isinstance(value, list):
- value = [d.as_dict() for d in value]
-
- context[fieldname] = value
-
- if self.slideshow and context.variant and context.variant.slideshow:
- context.update(get_slideshow(context.variant))
-
-
- def set_attribute_context(self, context):
- if not self.has_variants:
- return
-
- attribute_values_available = {}
- context.attribute_values = {}
- context.selected_attributes = {}
-
- # load attributes
- self.set_selected_attributes(context.variants, context, attribute_values_available)
-
- # filter attributes, order based on attribute table
- item = frappe.get_cached_doc("Item", self.item_code)
- self.set_attribute_values(item.attributes, context, attribute_values_available)
-
- context.variant_info = json.dumps(context.variants)
-
def set_selected_attributes(self, variants, context, attribute_values_available):
for variant in variants:
variant.attributes = frappe.get_all(
@@ -328,50 +272,6 @@
if attr_value.attribute_value in attribute_values_available.get(attr.attribute, []):
values.append(attr_value.attribute_value)
- def set_disabled_attributes(self, context):
- """Disable selection options of attribute combinations that do not result in a variant"""
-
- if not self.attributes or not self.has_variants:
- return
-
- context.disabled_attributes = {}
- attributes = [attr.attribute for attr in self.attributes]
-
- def find_variant(combination):
- for variant in context.variants:
- if len(variant.attributes) < len(attributes):
- continue
-
- if "combination" not in variant:
- ref_combination = []
-
- for attr in variant.attributes:
- idx = attributes.index(attr.attribute)
- ref_combination.insert(idx, attr.attribute_value)
-
- variant["combination"] = ref_combination
-
- if not (set(combination) - set(variant["combination"])):
- # check if the combination is a subset of a variant combination
- # eg. [Blue, 0.5] is a possible combination if exists [Blue, Large, 0.5]
- return True
-
- for i, attr in enumerate(self.attributes):
- if i == 0:
- continue
-
- combination_source = []
-
- # loop through previous attributes
- for prev_attr in self.attributes[:i]:
- combination_source.append([context.selected_attributes.get(prev_attr.attribute)])
-
- combination_source.append(context.attribute_values[attr.attribute])
-
- for combination in itertools.product(*combination_source):
- if not find_variant(combination):
- context.disabled_attributes.setdefault(attr.attribute, []).append(combination[-1])
-
def set_metatags(self, context):
context.metatags = frappe._dict({})
diff --git a/erpnext/e_commerce/variant_selector/item_variants_cache.py b/erpnext/e_commerce/variant_selector/item_variants_cache.py
index 39eb915..bb6b3ef 100644
--- a/erpnext/e_commerce/variant_selector/item_variants_cache.py
+++ b/erpnext/e_commerce/variant_selector/item_variants_cache.py
@@ -44,7 +44,7 @@
val = frappe.cache().get_value('ordered_attribute_values_map')
if val: return val
- all_attribute_values = frappe.db.get_all('Item Attribute Value',
+ all_attribute_values = frappe.get_all('Item Attribute Value',
['attribute_value', 'idx', 'parent'], order_by='idx asc')
ordered_attribute_values_map = frappe._dict({})
@@ -57,25 +57,34 @@
def build_cache(self):
parent_item_code = self.item_code
- attributes = [a.attribute for a in frappe.db.get_all('Item Variant Attribute',
- {'parent': parent_item_code}, ['attribute'], order_by='idx asc')
+ attributes = [
+ a.attribute for a in frappe.get_all(
+ 'Item Variant Attribute',
+ {'parent': parent_item_code},
+ ['attribute'],
+ order_by='idx asc'
+ )
]
- item_variants_data = frappe.db.get_all('Item Variant Attribute',
- {'variant_of': parent_item_code}, ['parent', 'attribute', 'attribute_value'],
+ # join with Website Item
+ item_variants_data = frappe.get_all(
+ 'Item Variant Attribute',
+ {'variant_of': parent_item_code},
+ ['parent', 'attribute', 'attribute_value'],
order_by='name',
as_list=1
)
- unpublished_items = set([i.item_code for i in frappe.db.get_all('Website Item', filters={'published': 0}, fields=["item_code"])])
+ disabled_items = set(
+ [i.name for i in frappe.db.get_all('Item', {'disabled': 1})]
+ )
- attribute_value_item_map = frappe._dict({})
- item_attribute_value_map = frappe._dict({})
+ attribute_value_item_map = frappe._dict()
+ item_attribute_value_map = frappe._dict()
- # dont consider variants that are unpublished
- # (either have no Website Item or are unpublished in Website Item)
- item_variants_data = [r for r in item_variants_data if r[0] not in unpublished_items]
- item_variants_data = [r for r in item_variants_data if frappe.db.exists("Website Item", {"item_code": r[0]})]
+ # dont consider variants that are disabled
+ # pull all other variants
+ item_variants_data = [r for r in item_variants_data if r[0] not in disabled_items]
for row in item_variants_data:
item_code, attribute, attribute_value = row