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 | |
marination | 7d1df9d | 2021-08-13 14:23:52 +0530 | [diff] [blame] | 5 | import json |
marination | 9fb61ef | 2022-02-01 00:39:14 +0530 | [diff] [blame] | 6 | |
| 7 | import frappe |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 8 | from frappe.utils import cint |
| 9 | |
marination | 335a237 | 2021-08-12 19:01:10 +0530 | [diff] [blame] | 10 | from erpnext.e_commerce.product_data_engine.filters import ProductFiltersBuilder |
marination | 9fb61ef | 2022-02-01 00:39:14 +0530 | [diff] [blame] | 11 | from erpnext.e_commerce.product_data_engine.query import ProductQuery |
marination | 80fbe16 | 2021-08-17 00:48:36 +0530 | [diff] [blame] | 12 | from erpnext.setup.doctype.item_group.item_group import get_child_groups_for_website |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 13 | |
marination | 9fb61ef | 2022-02-01 00:39:14 +0530 | [diff] [blame] | 14 | |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 15 | @frappe.whitelist(allow_guest=True) |
marination | 7d1df9d | 2021-08-13 14:23:52 +0530 | [diff] [blame] | 16 | def get_product_filter_data(query_args=None): |
marination | 80fbe16 | 2021-08-17 00:48:36 +0530 | [diff] [blame] | 17 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 18 | Returns filtered products and discount filters. |
| 19 | :param query_args (dict): contains filters to get products list |
marination | 80fbe16 | 2021-08-17 00:48:36 +0530 | [diff] [blame] | 20 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 21 | Query Args filters: |
| 22 | search (str): Search Term. |
| 23 | field_filters (dict): Keys include item_group, brand, etc. |
| 24 | attribute_filters(dict): Keys include Color, Size, etc. |
| 25 | start (int): Offset items by |
| 26 | item_group (str): Valid Item Group |
| 27 | from_filters (bool): Set as True to jump to page 1 |
marination | 80fbe16 | 2021-08-17 00:48:36 +0530 | [diff] [blame] | 28 | """ |
marination | 7d1df9d | 2021-08-13 14:23:52 +0530 | [diff] [blame] | 29 | if isinstance(query_args, str): |
| 30 | query_args = json.loads(query_args) |
| 31 | |
marination | 942dedd | 2021-09-03 17:50:14 +0530 | [diff] [blame] | 32 | query_args = frappe._dict(query_args) |
marination | 7d1df9d | 2021-08-13 14:23:52 +0530 | [diff] [blame] | 33 | if query_args: |
| 34 | search = query_args.get("search") |
| 35 | field_filters = query_args.get("field_filters", {}) |
| 36 | attribute_filters = query_args.get("attribute_filters", {}) |
| 37 | start = cint(query_args.start) if query_args.get("start") else 0 |
| 38 | item_group = query_args.get("item_group") |
| 39 | from_filters = query_args.get("from_filters") |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 40 | else: |
marination | c0811c4 | 2021-07-08 19:34:07 +0530 | [diff] [blame] | 41 | search, attribute_filters, item_group, from_filters = None, None, None, None |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 42 | field_filters = {} |
| 43 | start = 0 |
| 44 | |
marination | 7d1df9d | 2021-08-13 14:23:52 +0530 | [diff] [blame] | 45 | # if new filter is checked, reset start to show filtered items from page 1 |
marination | c0811c4 | 2021-07-08 19:34:07 +0530 | [diff] [blame] | 46 | if from_filters: |
marination | c0811c4 | 2021-07-08 19:34:07 +0530 | [diff] [blame] | 47 | start = 0 |
| 48 | |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 49 | sub_categories = [] |
| 50 | if item_group: |
marination | 80fbe16 | 2021-08-17 00:48:36 +0530 | [diff] [blame] | 51 | sub_categories = get_child_groups_for_website(item_group, immediate=True) |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 52 | |
| 53 | engine = ProductQuery() |
marination | 7d1df9d | 2021-08-13 14:23:52 +0530 | [diff] [blame] | 54 | try: |
| 55 | result = engine.query( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 56 | attribute_filters, field_filters, search_term=search, start=start, item_group=item_group |
marination | 7d1df9d | 2021-08-13 14:23:52 +0530 | [diff] [blame] | 57 | ) |
marination | 80fbe16 | 2021-08-17 00:48:36 +0530 | [diff] [blame] | 58 | except Exception: |
Rushabh Mehta | 548afba | 2022-05-02 15:04:26 +0530 | [diff] [blame] | 59 | frappe.log_error("Product query with filter failed") |
marination | 7d1df9d | 2021-08-13 14:23:52 +0530 | [diff] [blame] | 60 | return {"exc": "Something went wrong!"} |
marination | 0dadf53 | 2021-06-29 11:22:27 +0530 | [diff] [blame] | 61 | |
| 62 | # discount filter data |
| 63 | filters = {} |
| 64 | discounts = result["discounts"] |
| 65 | |
| 66 | if discounts: |
| 67 | filter_engine = ProductFiltersBuilder() |
| 68 | filters["discount_filters"] = filter_engine.get_discount_filters(discounts) |
| 69 | |
| 70 | return { |
| 71 | "items": result["items"] or [], |
| 72 | "filters": filters, |
| 73 | "settings": engine.settings, |
| 74 | "sub_categories": sub_categories, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 75 | "items_count": result["items_count"], |
marination | 82f8f3c | 2021-07-08 10:57:01 +0530 | [diff] [blame] | 76 | } |
| 77 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 78 | |
marination | 82f8f3c | 2021-07-08 10:57:01 +0530 | [diff] [blame] | 79 | @frappe.whitelist(allow_guest=True) |
| 80 | def get_guest_redirect_on_action(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 81 | return frappe.db.get_single_value("E Commerce Settings", "redirect_on_action") |