def add_cart_to_context(context): if locals().get('context.items') is not None: #frappe_log_error("[kalla] item found!") for item in context.items: context['shopping_cart'] = get_product_info_for_website(item.item_code, skip_quotation_creation=True) break
def get_context(context): 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) else: search = field_filters = attribute_filters = None context.products = get_products_for_website(field_filters, attribute_filters, search) for item in context.products: item["active_batch"] = get_active_batch(item.name) item["info"] = get_product_info_for_website(item.name) product_settings = get_product_settings() context.field_filters = get_field_filter_data() \ if product_settings.enable_field_filters else [] context.attribute_filters = get_attribute_filter_data() \ if product_settings.enable_attribute_filters else [] context.product_settings = product_settings context.page_length = product_settings.products_per_page context.banner_image = frappe.db.get_single_value('Products Settings', 'banner_image') context.no_cache = 1
def get_context(context): 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) else: search = field_filters = attribute_filters = None context.items = get_products_for_website(field_filters, attribute_filters, search) for item in context.items: frappe_log_error("[kalla] item found!") context['shopping_cart'] = get_product_info_for_website(item.item_code, skip_quotation_creation=True) break product_settings = get_product_settings() context.field_filters = get_field_filter_data() \ if product_settings.enable_field_filters else [] context.attribute_filters = get_attribute_filter_data() \ if product_settings.enable_attribute_filters else [] context.product_settings = product_settings context.page_length = product_settings.products_per_page context.no_cache = 1
def query(self, attributes=None, fields=None, search_term=None, start=0): """Summary Args: attributes (dict, optional): Item Attribute filters fields (dict, optional): Field level filters search_term (str, optional): Search term to lookup start (int, optional): Page start Returns: list: List of results with set fields """ if fields: self.build_fields_filters(fields) if search_term: self.build_search_filters(search_term) result = [] if attributes: all_items = [] for attribute, values in attributes.items(): if not isinstance(values, list): values = [values] items = frappe.get_all( "Item", fields=self.fields, filters=[ *self.filters, ["Item Variant Attribute", "attribute", "=", attribute], ["Item Variant Attribute", "attribute_value", "in", values], ], or_filters=self.or_filters, start=start, limit=self.page_length ) items_dict = {item.name: item for item in items} # TODO: Replace Variants by their parent templates all_items.append(set(items_dict.keys())) result = [items_dict.get(item) for item in list(set.intersection(*all_items))] else: result = frappe.get_all("Item", fields=self.fields, filters=self.filters, or_filters=self.or_filters, start=start, limit=self.page_length) for item in result: product_info = get_product_info_for_website( item.item_code, skip_quotation_creation=True).get('product_info') if product_info: item.formatted_price = product_info['price'].get( 'formatted_price') if product_info['price'] else None return result
def get_product_info(route): item_code = frappe.db.exists("Item", { "route": route.replace("__", "/"), "show_in_website": 1 }) if not item_code: frappe.throw(frappe._("Item does not exist at this route")) item_for_website = get_product_info_for_website(item_code) return { "price": pick( ["currency", "price_list_rate"], item_for_website.get("product_info", {}).get("price", {}), ) }
def get_available_item(item, start, end): alternative_items = frappe.get_all("Item", \ filters={"show_in_website": 1, "enable_item_booking": 1, "item_code": ["!=", item]}, \ fields=["name", "item_name", "route", "website_image", "image", "description", "website_content"]) available_items = [] for alternative_item in alternative_items: availabilities = get_availabilities(alternative_item.name, start, end) or [] if len(availabilities): available_items.append(alternative_item) result = [] for available_item in available_items: product_info = get_product_info_for_website(available_item.name) if product_info.product_info and product_info.product_info.get("price") \ and (product_info.cart_settings.get("allow_items_not_in_stock") \ or product_info.product_info.get("in_stock")): result.append(available_item) return result
def get_next_attribute_and_values(item_code, selected_attributes): '''Find the count of Items that match the selected attributes. Also, find the attribute values that are not applicable for further searching. If less than equal to 10 items are found, return item_codes of those items. If one item is matched exactly, return item_code of that item. ''' selected_attributes = frappe.parse_json(selected_attributes) item_cache = ItemVariantsCacheManager(item_code) item_variants_data = item_cache.get_item_variants_data() attributes = get_item_attributes(item_code) attribute_list = [a.attribute for a in attributes] filtered_items = get_items_with_selected_attributes( item_code, selected_attributes) next_attribute = None for attribute in attribute_list: if attribute not in selected_attributes: next_attribute = attribute break valid_options_for_attributes = frappe._dict({}) for a in attribute_list: valid_options_for_attributes[a] = set() selected_attribute = selected_attributes.get(a, None) if selected_attribute: # already selected attribute values are valid options valid_options_for_attributes[a].add(selected_attribute) for row in item_variants_data: item_code, attribute, attribute_value = row if item_code in filtered_items and attribute not in selected_attributes and attribute in attribute_list: valid_options_for_attributes[attribute].add(attribute_value) optional_attributes = item_cache.get_optional_attributes() exact_match = [] shopping_cart_settings = get_shopping_cart_settings() allow_items_not_in_stock = cint( shopping_cart_settings.allow_items_not_in_stock) # search for exact match if all selected attributes are required attributes if len(selected_attributes.keys()) >= (len(attribute_list) - len(optional_attributes)): item_attribute_value_map = item_cache.get_item_attribute_value_map() for item_code, attr_dict in item_attribute_value_map.items(): if item_code in filtered_items and set(attr_dict.keys()) == set( selected_attributes.keys()): exact_match.append(item_code) filtered_items_count = len(filtered_items) # get product info if exact match from erpnext.shopping_cart.product_info import get_product_info_for_website if exact_match: data = get_product_info_for_website(exact_match[0]) product_info = data.product_info if not data.cart_settings.show_price: product_info = None else: product_info = None return { 'next_attribute': next_attribute, 'valid_options_for_attributes': valid_options_for_attributes, 'filtered_items_count': filtered_items_count, 'filtered_items': filtered_items if filtered_items_count < 10 else [], 'exact_match': exact_match, 'allow_items_not_in_stock': allow_items_not_in_stock, 'product_info': product_info }
def set_shopping_cart_data(self, context): from erpnext.shopping_cart.product_info import get_product_info_for_website context.shopping_cart = get_product_info_for_website(self.name, skip_quotation_creation=True)
def get_shopping_cart(item_code): return get_product_info_for_website(item_code, skip_quotation_creation=True)
def set_shopping_cart_data(self, context): from erpnext.shopping_cart.product_info import get_product_info_for_website context.shopping_cart = get_product_info_for_website(self.name)
def get_items(filters=None, search=None): start = frappe.form_dict.start or 0 products_settings = get_product_settings() page_length = products_settings.products_per_page filters = filters or [] # convert to list of filters if isinstance(filters, dict): filters = [['Item', fieldname, '=', value] for fieldname, value in filters.items()] enabled_items_filter = get_conditions({'disabled': 0}, 'and') show_in_website_condition = '' if products_settings.hide_variants: show_in_website_condition = get_conditions({'show_in_website': 1}, 'and') else: show_in_website_condition = get_conditions( [['show_in_website', '=', 1], ['show_variant_in_website', '=', 1]], 'or') search_condition = '' if search: # Default fields to search from default_fields = {'name', 'item_name', 'description', 'item_group'} # Get meta search fields meta = frappe.get_meta("Item") meta_fields = set(meta.get_search_fields()) # Join the meta fields and default fields set search_fields = default_fields.union(meta_fields) try: if frappe.db.count('Item', cache=True) > 50000: search_fields.remove('description') except KeyError: pass # Build or filters for query search = '%{}%'.format(search) or_filters = [[field, 'like', search] for field in search_fields] search_condition = get_conditions(or_filters, 'or') filter_condition = get_conditions(filters, 'and') where_conditions = ' and '.join( [condition for condition in [enabled_items_filter, show_in_website_condition, \ search_condition, filter_condition] if condition] ) left_joins = [] for f in filters: if len(f) == 4 and f[0] != 'Item': left_joins.append(f[0]) left_join = ' '.join([ 'LEFT JOIN `tab{0}` on (`tab{0}`.parent = `tabItem`.name)'.format(l) for l in left_joins ]) results = frappe.db.sql(''' SELECT `tabItem`.`name`, `tabItem`.`item_name`, `tabItem`.`item_code`, `tabItem`.`website_image`, `tabItem`.`image`, `tabItem`.`web_long_description`, `tabItem`.`description`, `tabItem`.`route`, `tabItem`.`item_group` FROM `tabItem` {left_join} WHERE {where_conditions} GROUP BY `tabItem`.`name` ORDER BY `tabItem`.`weightage` DESC LIMIT {page_length} OFFSET {start} '''.format(where_conditions=where_conditions, start=start, page_length=page_length, left_join=left_join), as_dict=1) for r in results: r.description = r.web_long_description or r.description r.image = r.website_image or r.image product_info = get_product_info_for_website( r.item_code, skip_quotation_creation=True).get('product_info') r.formatted_price = product_info['price'].get( 'formatted_price') if product_info['price'] else None return results