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
|
|
|
|
|
2021-08-13 08:53:52 +00:00
|
|
|
import json
|
2022-01-31 19:09:14 +00:00
|
|
|
|
|
|
|
import frappe
|
2021-06-29 05:52:27 +00:00
|
|
|
from frappe.utils import cint
|
|
|
|
|
2021-08-12 13:31:10 +00:00
|
|
|
from erpnext.e_commerce.product_data_engine.filters import ProductFiltersBuilder
|
2022-01-31 19:09:14 +00:00
|
|
|
from erpnext.e_commerce.product_data_engine.query import ProductQuery
|
2021-08-16 19:18:36 +00:00
|
|
|
from erpnext.setup.doctype.item_group.item_group import get_child_groups_for_website
|
2021-06-29 05:52:27 +00:00
|
|
|
|
2022-01-31 19:09:14 +00:00
|
|
|
|
2021-06-29 05:52:27 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
2021-08-13 08:53:52 +00:00
|
|
|
def get_product_filter_data(query_args=None):
|
2021-08-16 19:18:36 +00:00
|
|
|
"""
|
|
|
|
Returns filtered products and discount filters.
|
|
|
|
:param query_args (dict): contains filters to get products list
|
|
|
|
|
|
|
|
Query Args filters:
|
|
|
|
search (str): Search Term.
|
|
|
|
field_filters (dict): Keys include item_group, brand, etc.
|
|
|
|
attribute_filters(dict): Keys include Color, Size, etc.
|
|
|
|
start (int): Offset items by
|
|
|
|
item_group (str): Valid Item Group
|
|
|
|
from_filters (bool): Set as True to jump to page 1
|
|
|
|
"""
|
2021-08-13 08:53:52 +00:00
|
|
|
if isinstance(query_args, str):
|
|
|
|
query_args = json.loads(query_args)
|
|
|
|
|
2021-09-03 12:20:14 +00:00
|
|
|
query_args = frappe._dict(query_args)
|
2021-08-13 08:53:52 +00:00
|
|
|
if query_args:
|
|
|
|
search = query_args.get("search")
|
|
|
|
field_filters = query_args.get("field_filters", {})
|
|
|
|
attribute_filters = query_args.get("attribute_filters", {})
|
|
|
|
start = cint(query_args.start) if query_args.get("start") else 0
|
|
|
|
item_group = query_args.get("item_group")
|
|
|
|
from_filters = query_args.get("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-08-13 08:53:52 +00:00
|
|
|
# if new filter is checked, reset start to show filtered items from page 1
|
2021-07-08 14:04:07 +00:00
|
|
|
if from_filters:
|
|
|
|
start = 0
|
|
|
|
|
2021-06-29 05:52:27 +00:00
|
|
|
sub_categories = []
|
|
|
|
if item_group:
|
2021-08-16 19:18:36 +00:00
|
|
|
sub_categories = get_child_groups_for_website(item_group, immediate=True)
|
2021-06-29 05:52:27 +00:00
|
|
|
|
|
|
|
engine = ProductQuery()
|
2021-08-13 08:53:52 +00:00
|
|
|
try:
|
|
|
|
result = engine.query(
|
|
|
|
attribute_filters, field_filters, search_term=search, start=start, item_group=item_group
|
|
|
|
)
|
2021-08-16 19:18:36 +00:00
|
|
|
except Exception:
|
2022-05-02 09:34:26 +00:00
|
|
|
frappe.log_error("Product query with filter failed")
|
2021-08-13 08:53:52 +00:00
|
|
|
return {"exc": "Something went wrong!"}
|
2021-06-29 05:52:27 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
}
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
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")
|