From 34f3a6629fcc7de874d4e342351eaeb98fcac629 Mon Sep 17 00:00:00 2001 From: Hussain Nagaria Date: Wed, 5 May 2021 13:47:43 +0530 Subject: [PATCH] feat: Show brand line in search results --- .../e_commerce_settings/e_commerce_settings.json | 4 ++-- .../e_commerce_settings/e_commerce_settings.py | 6 ++++++ erpnext/e_commerce/website_item_indexing.py | 12 ++++++++---- erpnext/www/all-products/search.css | 4 ++++ erpnext/www/all-products/search.html | 6 +++++- erpnext/www/all-products/search.js | 3 ++- 6 files changed, 27 insertions(+), 8 deletions(-) diff --git a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json index 0f7b2cc4f0..36177ff8f9 100644 --- a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json +++ b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json @@ -323,7 +323,7 @@ "label": "Show Categories in Search Autocomplete" }, { - "default": "1", + "default": "0", "description": "e.g. \"iPhone 12 by Apple\"", "fieldname": "show_brand_line", "fieldtype": "Check", @@ -333,7 +333,7 @@ "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2021-05-05 13:05:52.615719", + "modified": "2021-05-05 13:41:11.483232", "modified_by": "Administrator", "module": "E-commerce", "name": "E Commerce Settings", diff --git a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py index a028a5f86f..441e85b645 100644 --- a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py +++ b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.py @@ -25,7 +25,9 @@ class ECommerceSettings(Document): self.validate_field_filters() self.validate_attribute_filters() self.validate_checkout() + self.validate_brand_check() self.validate_search_index_fields() + if self.enabled: self.validate_exchange_rates_exist() @@ -76,6 +78,10 @@ class ECommerceSettings(Document): self.search_index_fields = ','.join(fields) + def validate_brand_check(self): + if self.show_brand_line and not ("brand" in self.search_index_fields): + self.search_index_fields += ",brand" + def validate_exchange_rates_exist(self): """check if exchange rates exist for all Price List currencies (to company's currency)""" company_currency = frappe.get_cached_value('Company', self.company, "default_currency") diff --git a/erpnext/e_commerce/website_item_indexing.py b/erpnext/e_commerce/website_item_indexing.py index 4e3af8bf1f..8c0e074dc3 100644 --- a/erpnext/e_commerce/website_item_indexing.py +++ b/erpnext/e_commerce/website_item_indexing.py @@ -99,8 +99,6 @@ def update_index_for_item(website_item_doc): # Reinsert to Cache insert_item_to_index(website_item_doc) define_autocomplete_dictionary() - # TODO: Only reindex updated items - create_website_items_index() def delete_item_from_index(website_item_doc): r = frappe.cache() @@ -111,9 +109,16 @@ def delete_item_from_index(website_item_doc): except: return False - # TODO: Also delete autocomplete suggestion + delete_from_ac_dict(website_item_doc) + return True +def delete_from_ac_dict(website_item_doc): + '''Removes this items's name from autocomplete dictionary''' + r = frappe.cache() + name_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=r) + name_ac.delete(website_item_doc.web_item_name) + def define_autocomplete_dictionary(): """Creates an autocomplete search dictionary for `name`. Also creats autocomplete dictionary for `categories` if @@ -163,7 +168,6 @@ def reindex_all_web_items(): for k, v in web_item.items(): super(RedisWrapper, r).hset(key, k, v) - def get_cache_key(name): name = frappe.scrub(name) return f"{WEBSITE_ITEM_KEY_PREFIX}{name}" diff --git a/erpnext/www/all-products/search.css b/erpnext/www/all-products/search.css index ff0ca6ac9f..687532d2eb 100644 --- a/erpnext/www/all-products/search.css +++ b/erpnext/www/all-products/search.css @@ -2,4 +2,8 @@ height: 50px; width: 50px; object-fit: cover; +} + +.brand-line { + color: gray; } \ No newline at end of file diff --git a/erpnext/www/all-products/search.html b/erpnext/www/all-products/search.html index c6c87089e8..a86a9c0e13 100644 --- a/erpnext/www/all-products/search.html +++ b/erpnext/www/all-products/search.html @@ -26,7 +26,6 @@ {% set show_categories = frappe.db.get_single_value('E Commerce Settings', 'show_categories_in_search_autocomplete') %} - {% if show_categories %}

Categories

@@ -34,6 +33,11 @@
{% endif %} + + {% set show_brand_line = frappe.db.get_single_value('E Commerce Settings', 'show_brand_line') %} + {% if show_brand_line %} + + {% endif %} {% endblock %} \ No newline at end of file diff --git a/erpnext/www/all-products/search.js b/erpnext/www/all-products/search.js index 9ed5b21099..c97e48b8db 100644 --- a/erpnext/www/all-products/search.js +++ b/erpnext/www/all-products/search.js @@ -3,13 +3,14 @@ let loading = false; const searchBox = document.getElementById("search-box"); const results = document.getElementById("results"); const categoryList = document.getElementById("category-suggestions"); +const showBrandLine = document.getElementById("show-brand-line"); function populateResults(data) { html = "" for (let res of data.message) { html += `
  • - ${res.web_item_name} + ${res.web_item_name} ${showBrandLine && res.brand ? "by " + res.brand : ""}
  • ` } results.innerHTML = html;