2021-06-29 05:52:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
|
|
|
import frappe
|
|
|
|
from frappe.utils import cint
|
|
|
|
|
2021-08-12 13:31:10 +00:00
|
|
|
from erpnext.e_commerce.product_data_engine.query import ProductQuery
|
|
|
|
from erpnext.e_commerce.product_data_engine.filters import ProductFiltersBuilder
|
2021-06-29 05:52:27 +00:00
|
|
|
from erpnext.setup.doctype.item_group.item_group import get_child_groups
|
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_product_filter_data():
|
|
|
|
"""Get pre-rendered filtered products and discount filters on load."""
|
|
|
|
if frappe.form_dict:
|
|
|
|
search = frappe.form_dict.search
|
|
|
|
field_filters = frappe.parse_json(frappe.form_dict.field_filters)
|
|
|
|
attribute_filters = frappe.parse_json(frappe.form_dict.attribute_filters)
|
|
|
|
start = cint(frappe.parse_json(frappe.form_dict.start)) if frappe.form_dict.start else 0
|
|
|
|
item_group = frappe.form_dict.item_group
|
2021-07-08 14:04:07 +00:00
|
|
|
from_filters = frappe.parse_json(frappe.form_dict.from_filters)
|
2021-06-29 05:52:27 +00:00
|
|
|
else:
|
2021-07-08 14:04:07 +00:00
|
|
|
search, attribute_filters, item_group, from_filters = None, None, None, None
|
2021-06-29 05:52:27 +00:00
|
|
|
field_filters = {}
|
|
|
|
start = 0
|
|
|
|
|
2021-07-08 14:04:07 +00:00
|
|
|
if from_filters:
|
|
|
|
# if filter is checked, go to start
|
|
|
|
# and show filtered items from page 1
|
|
|
|
start = 0
|
|
|
|
|
2021-06-29 05:52:27 +00:00
|
|
|
sub_categories = []
|
|
|
|
if item_group:
|
|
|
|
field_filters['item_group'] = item_group
|
|
|
|
sub_categories = get_child_groups(item_group)
|
|
|
|
|
|
|
|
engine = ProductQuery()
|
|
|
|
result = engine.query(attribute_filters, field_filters, search_term=search,
|
|
|
|
start=start, item_group=item_group)
|
|
|
|
|
|
|
|
# discount filter data
|
|
|
|
filters = {}
|
|
|
|
discounts = result["discounts"]
|
|
|
|
|
|
|
|
if discounts:
|
|
|
|
filter_engine = ProductFiltersBuilder()
|
|
|
|
filters["discount_filters"] = filter_engine.get_discount_filters(discounts)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"items": result["items"] or [],
|
|
|
|
"filters": filters,
|
|
|
|
"settings": engine.settings,
|
|
|
|
"sub_categories": sub_categories,
|
|
|
|
"items_count": result["items_count"]
|
2021-07-08 05:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_guest_redirect_on_action():
|
|
|
|
return frappe.db.get_single_value("E Commerce Settings", "redirect_on_action")
|