From 333d08f4592ae49f227d7ae6c97a3551b28bfc6f Mon Sep 17 00:00:00 2001 From: marination Date: Fri, 19 Feb 2021 15:56:52 +0530 Subject: [PATCH] fix: Hide Attribute filters if 'Hide Variants' is enabled in E Commerce Settings - Hide Attribute filters if 'Hide Variants' is enabled in E Commerce Settings - Consider 'Hide Variants' in ProductQuery Engine - Added docstrings - Remove `get_e_commerce_settings`, redundant --- .../doctype/website_item/website_item.py | 1 + erpnext/portal/product_configurator/utils.py | 12 -- erpnext/shopping_cart/filters.py | 4 + erpnext/shopping_cart/product_query.py | 6 +- erpnext/stock/doctype/item/item.py | 3 +- erpnext/www/all-products/index.html | 112 +++++++++--------- 6 files changed, 70 insertions(+), 68 deletions(-) diff --git a/erpnext/e_commerce/doctype/website_item/website_item.py b/erpnext/e_commerce/doctype/website_item/website_item.py index 53ed5e88911..64655716b46 100644 --- a/erpnext/e_commerce/doctype/website_item/website_item.py +++ b/erpnext/e_commerce/doctype/website_item/website_item.py @@ -326,6 +326,7 @@ class WebsiteItem(WebsiteGenerator): row.description = desc def invalidate_cache_for_web_item(doc): + """Invalidate Website Item Group cache and rebuild ItemVariantsCacheManager.""" from erpnext.stock.doctype.item.item import invalidate_item_variants_cache_for_website invalidate_cache_for(doc, doc.item_group) diff --git a/erpnext/portal/product_configurator/utils.py b/erpnext/portal/product_configurator/utils.py index b9e05caa755..cca36025160 100644 --- a/erpnext/portal/product_configurator/utils.py +++ b/erpnext/portal/product_configurator/utils.py @@ -136,15 +136,3 @@ def get_item_attributes(item_code): return attributes -def get_html_for_items(items): - html = [] - for item in items: - html.append(frappe.render_template('erpnext/www/all-products/item_row.html', { - 'item': item - })) - return html - -def get_e_commerce_settings(): - doc = frappe.get_cached_doc('E Commerce Settings') - doc.products_per_page = doc.products_per_page or 20 - return doc diff --git a/erpnext/shopping_cart/filters.py b/erpnext/shopping_cart/filters.py index a8a65beb96f..4021e3f86f7 100644 --- a/erpnext/shopping_cart/filters.py +++ b/erpnext/shopping_cart/filters.py @@ -15,6 +15,8 @@ class ProductFiltersBuilder: self.item_group = item_group def get_field_filters(self): + if not self.doc.enable_field_filters: return + filter_fields = [row.fieldname for row in self.doc.filter_fields] meta = frappe.get_meta('Item') @@ -52,6 +54,8 @@ class ProductFiltersBuilder: return filter_data def get_attribute_fitlers(self): + if not self.doc.enable_attribute_filters: return + attributes = [row.attribute for row in self.doc.filter_attributes] attribute_docs = [ frappe.get_doc('Item Attribute', attribute) for attribute in attributes diff --git a/erpnext/shopping_cart/product_query.py b/erpnext/shopping_cart/product_query.py index 04e42b4caea..42152a15a92 100644 --- a/erpnext/shopping_cart/product_query.py +++ b/erpnext/shopping_cart/product_query.py @@ -40,9 +40,11 @@ class ProductQuery: if fields: self.build_fields_filters(fields) if search_term: self.build_search_filters(search_term) + if self.settings.hide_variants: + self.conditions += " and wi.variant_of is null" + result = [] - self.query_fields = (", ").join(self.fields) if attributes: result = self.query_items_with_attributes(attributes, start) else: @@ -59,6 +61,8 @@ class ProductQuery: def query_items(self, conditions, or_conditions, substitutions, start=0): """Build a query to fetch Website Items based on field filters.""" + self.query_fields = (", ").join(self.fields) + return frappe.db.sql(""" select distinct {query_fields} from diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index 414837d754f..d6d7c56ad1d 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -18,7 +18,6 @@ from frappe.utils.html_utils import clean_html from frappe.website.render import clear_cache from frappe.model.document import Document -from frappe.website.website_generator import WebsiteGenerator from six import iteritems @@ -830,6 +829,7 @@ def get_last_purchase_details(item_code, doc_name=None, conversion_rate=1.0): def invalidate_cache_for_item(doc): + """Invalidate Item Group cache and rebuild ItemVariantsCacheManager.""" invalidate_cache_for(doc, doc.item_group) if doc.get("old_item_group") and doc.get("old_item_group") != doc.item_group: @@ -839,6 +839,7 @@ def invalidate_cache_for_item(doc): def invalidate_item_variants_cache_for_website(doc): + """Rebuild ItemVariantsCacheManager via Item or Website Item.""" from erpnext.portal.product_configurator.item_variants_cache import ItemVariantsCacheManager item_code = None diff --git a/erpnext/www/all-products/index.html b/erpnext/www/all-products/index.html index 92c76ad8790..7dfce210b81 100644 --- a/erpnext/www/all-products/index.html +++ b/erpnext/www/all-products/index.html @@ -53,68 +53,72 @@
{{ _('Filters') }}
{{ _('Clear All') }} - {% for field_filter in field_filters %} - {%- set item_field = field_filter[0] %} - {%- set values = field_filter[1] %} -
-
{{ item_field.label }}
+ {% if field_filters %} + {% for field_filter in field_filters %} + {%- set item_field = field_filter[0] %} + {%- set values = field_filter[1] %} +
+
{{ item_field.label }}
- {% if values | len > 20 %} - - - {% endif %} + {% if values | len > 20 %} + + + {% endif %} - {% if values %} -
- {% for value in values %} -
- + {% if values %} +
+ {% for value in values %} +
+ +
+ {% endfor %}
- {% endfor %} + {% else %} + {{ _('No values') }} + {% endif %}
- {% else %} - {{ _('No values') }} - {% endif %} -
- {% endfor %} + {% endfor %} + {% endif %} - {% for attribute in attribute_filters %} -
-
{{ attribute.name}}
- {% if values | len > 20 %} - - - {% endif %} + {% if attribute_filters %} + {% for attribute in attribute_filters %} +
+
{{ attribute.name}}
+ {% if values | len > 20 %} + + + {% endif %} - {% if attribute.item_attribute_values %} -
- {% for attr_value in attribute.item_attribute_values %} -
- + {% if attribute.item_attribute_values %} +
+ {% for attr_value in attribute.item_attribute_values %} +
+ +
+ {% endfor %}
- {% endfor %} + {% else %} + {{ _('No values') }} + {% endif %}
- {% else %} - {{ _('No values') }} - {% endif %} -
- {% endfor %} + {% endfor %} + {% endif %}