feat: Show brand line in search results

This commit is contained in:
Hussain Nagaria 2021-05-05 13:47:43 +05:30 committed by marination
parent 9ee13ba86b
commit 34f3a6629f
6 changed files with 27 additions and 8 deletions

View File

@ -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",

View File

@ -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")

View File

@ -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}"

View File

@ -2,4 +2,8 @@
height: 50px;
width: 50px;
object-fit: cover;
}
.brand-line {
color: gray;
}

View File

@ -26,7 +26,6 @@
</div>
{% set show_categories = frappe.db.get_single_value('E Commerce Settings', 'show_categories_in_search_autocomplete') %}
{% if show_categories %}
<div id="categories" class="col-sm">
<h2>Categories</h2>
@ -34,6 +33,11 @@
</ul>
</div>
{% endif %}
{% set show_brand_line = frappe.db.get_single_value('E Commerce Settings', 'show_brand_line') %}
{% if show_brand_line %}
<span id="show-brand-line"></span>
{% endif %}
</div>
{% endblock %}

View File

@ -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 += `<li class="list-group-item list-group-item-action">
<img class="item-thumb" src="${res.thumbnail || 'img/placeholder.png'}" />
<a href="/${res.route}">${res.web_item_name}</a>
<a href="/${res.route}">${res.web_item_name} <span class="brand-line">${showBrandLine && res.brand ? "by " + res.brand : ""}</span></a>
</li>`
}
results.innerHTML = html;