diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2024-09-03 08:56:07 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2024-09-03 08:56:07 +0700 |
| commit | 643b93d9b57df8916714d898ba6457282d6756c2 (patch) | |
| tree | f42274455553cdc3d754212759c276975b829f90 | |
| parent | 9808cae54a32a65a41d0a13a141e62d88abedfef (diff) | |
trying to add category parent to solr product template
| -rw-r--r-- | indoteknik_custom/models/solr/product_template.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/indoteknik_custom/models/solr/product_template.py b/indoteknik_custom/models/solr/product_template.py index 6f76c529..2e34befe 100644 --- a/indoteknik_custom/models/solr/product_template.py +++ b/indoteknik_custom/models/solr/product_template.py @@ -101,6 +101,7 @@ class ProductTemplate(models.Model): "search_rank_i": template.search_rank, "search_rank_weekly_i": template.search_rank_weekly, "category_id_ids": category_ids, # ID kategori sebagai string yang dipisahkan koma + "category_parent_ids": self.get_category_hierarchy_ids(category_ids), # ID kategori sebagai string yang dipisahkan koma "category_name_s": ', '.join(category_names), # Nama kategori sebagai string yang dipisahkan koma "category_name": category_names, # Nama kategori sebagai list "description_t": template.website_description or '', @@ -127,6 +128,36 @@ class ProductTemplate(models.Model): if not document.get('has_price_info_b'): template._sync_price_to_solr() + def get_category_hierarchy_ids(category_id, env): + """ + Function to get category hierarchy IDs including the category itself and its parents. + + Args: + category_id (int): The ID of the category you want to retrieve. + env (Environment): The Odoo environment. + + Returns: + list: A list of IDs for the category and its parents. + """ + category_ids = [] + + def traverse_category(cat_id): + # Retrieve category object based on ID + category = env['product.category'].browse(cat_id) + + # Add the current category ID to the list + category_ids.append(category.id) + + # If there is a parent category, traverse upwards + if category.parent_id: + traverse_category(category.parent_id.id) + + # Start traversal from the initial category + traverse_category(category_id) + + # Reverse the list to get the hierarchy from top level to the current level + return list(reversed(category_ids)) + def _sync_price_to_solr(self): solr_model = self.env['apache.solr'] TIER_NUMBERS = ['1_v2', '2_v2', '3_v2', '4_v2', '5_v2'] |
