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