marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors |
| 3 | # For license information, please see license.txt |
| 4 | |
| 5 | import frappe |
| 6 | from frappe.utils import cint |
| 7 | |
| 8 | from erpnext.e_commerce.product_query import ProductQuery |
| 9 | from erpnext.e_commerce.filters import ProductFiltersBuilder |
| 10 | from erpnext.setup.doctype.item_group.item_group import get_child_groups |
| 11 | |
| 12 | @frappe.whitelist(allow_guest=True) |
| 13 | def get_product_filter_data(): |
| 14 | """Get pre-rendered filtered products and discount filters on load.""" |
| 15 | if frappe.form_dict: |
| 16 | search = frappe.form_dict.search |
| 17 | field_filters = frappe.parse_json(frappe.form_dict.field_filters) |
| 18 | attribute_filters = frappe.parse_json(frappe.form_dict.attribute_filters) |
| 19 | start = cint(frappe.parse_json(frappe.form_dict.start)) if frappe.form_dict.start else 0 |
| 20 | item_group = frappe.form_dict.item_group |
marination | c0811c4 | 2021-07-08 19:34:07 +0530 | [diff] [blame] | 21 | from_filters = frappe.parse_json(frappe.form_dict.from_filters) |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 22 | else: |
marination | c0811c4 | 2021-07-08 19:34:07 +0530 | [diff] [blame] | 23 | search, attribute_filters, item_group, from_filters = None, None, None, None |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 24 | field_filters = {} |
| 25 | start = 0 |
| 26 | |
marination | c0811c4 | 2021-07-08 19:34:07 +0530 | [diff] [blame] | 27 | if from_filters: |
| 28 | # if filter is checked, go to start |
| 29 | # and show filtered items from page 1 |
| 30 | start = 0 |
| 31 | |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 32 | sub_categories = [] |
| 33 | if item_group: |
| 34 | field_filters['item_group'] = item_group |
| 35 | sub_categories = get_child_groups(item_group) |
| 36 | |
| 37 | engine = ProductQuery() |
| 38 | result = engine.query(attribute_filters, field_filters, search_term=search, |
| 39 | start=start, item_group=item_group) |
| 40 | |
| 41 | # discount filter data |
| 42 | filters = {} |
| 43 | discounts = result["discounts"] |
| 44 | |
| 45 | if discounts: |
| 46 | filter_engine = ProductFiltersBuilder() |
| 47 | filters["discount_filters"] = filter_engine.get_discount_filters(discounts) |
| 48 | |
| 49 | return { |
| 50 | "items": result["items"] or [], |
| 51 | "filters": filters, |
| 52 | "settings": engine.settings, |
| 53 | "sub_categories": sub_categories, |
| 54 | "items_count": result["items_count"] |
marination | 82f8f3c | 2021-07-08 10:57:01 +0530 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @frappe.whitelist(allow_guest=True) |
| 58 | def get_guest_redirect_on_action(): |
| 59 | return frappe.db.get_single_value("E Commerce Settings", "redirect_on_action") |