diff --git a/product_attribute_groupby_filter/README.rst b/product_attribute_groupby_filter/README.rst new file mode 100644 index 00000000000..9ada21ff004 --- /dev/null +++ b/product_attribute_groupby_filter/README.rst @@ -0,0 +1,102 @@ +================================ +Product Attribute Groupby Filter +================================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:1fa29ea1eced64c6a99479bb021207efe4299dd2cf0f79110675289cab78d380 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fproduct--attribute-lightgray.png?logo=github + :target: https://github.com/OCA/product-attribute/tree/14.0/product_attribute_groupby_filter + :alt: OCA/product-attribute +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/product-attribute-14-0/product-attribute-14-0-product_attribute_groupby_filter + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/product-attribute&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds a **"Group by Attribute"** option to the searchbar of the +``product.product`` list view. It allows users to dynamically group product +variants by any attribute value (e.g. Size → S / M / L, Color → Red / Blue) +directly from the Group By menu, without requiring dedicated groupby fields for +each attribute. + +Up to **3 simultaneous attribute groupings** are supported. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +* **Dynamic Group By**: group ``product.product`` records by any + ``product.attribute`` value from the searchbar Group By menu. +* **Multi-level grouping**: combine up to 3 attribute groupings, optionally + combined with native Odoo groupBy fields. +* **Sort order**: attribute values can be sorted by *sequence* (default) or + *alphabetically*, configurable in settings. +* **Exclude from Group By**: individual attributes can be excluded from the + widget via a dedicated flag on the attribute form. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Akretion + +Contributors +~~~~~~~~~~~~ + +* Kévin Roche + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-Kev-Roche| image:: https://github.com/Kev-Roche.png?size=40px + :target: https://github.com/Kev-Roche + :alt: Kev-Roche + +Current `maintainer `__: + +|maintainer-Kev-Roche| + +This module is part of the `OCA/product-attribute `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/product_attribute_groupby_filter/__init__.py b/product_attribute_groupby_filter/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/product_attribute_groupby_filter/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/product_attribute_groupby_filter/__manifest__.py b/product_attribute_groupby_filter/__manifest__.py new file mode 100644 index 00000000000..9a3f732149d --- /dev/null +++ b/product_attribute_groupby_filter/__manifest__.py @@ -0,0 +1,28 @@ +# Copyright 2026 Akretion (https://www.akretion.com). +# @author Kévin Roche +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Product Attribute Groupby Filter", + "summary": "Allow grouping by attributes in product tree view.", + "version": "14.0.0.1.0", + "category": "product", + "website": "https://github.com/OCA/product-attribute", + "author": "Akretion, Odoo Community Association (OCA)", + "license": "AGPL-3", + "maintainers": ["Kev-Roche"], + "application": False, + "installable": True, + "depends": [ + "product", + ], + "data": [ + "views/assets.xml", + "views/product_attribute.xml", + "views/product_product.xml", + "views/res_config_settings.xml", + ], + "qweb": [ + "static/src/xml/product_attribute_groupby.xml", + ], +} diff --git a/product_attribute_groupby_filter/i18n/fr.po b/product_attribute_groupby_filter/i18n/fr.po new file mode 100644 index 00000000000..89de6f0302c --- /dev/null +++ b/product_attribute_groupby_filter/i18n/fr.po @@ -0,0 +1,176 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_attribute_groupby_filter +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2026-03-13 21:43+0000\n" +"PO-Revision-Date: 2026-03-13 22:48+0100\n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: fr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" +"X-Generator: Poedit 3.9\n" + +#. module: product_attribute_groupby_filter +#. openerp-web +#: code:addons/product_attribute_groupby_filter/static/src/xml/product_attribute_groupby.xml:0 +#, python-format +msgid "Apply" +msgstr "Appliquer" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_product_product__attribute_group_by_1 +msgid "Attribute Group By 1" +msgstr "" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_product_product__attribute_group_by_2 +msgid "Attribute Group By 2" +msgstr "" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_product_product__attribute_group_by_3 +msgid "Attribute Group By 3" +msgstr "" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_res_company__product_groupby_attribute_sort +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_res_config_settings__product_groupby_attribute_sort +msgid "Attribute Group By Sort Order" +msgstr "Ordre de Regrouper par Caractéristique" + +#. module: product_attribute_groupby_filter +#: model_terms:ir.ui.view,arch_db:product_attribute_groupby_filter.product_product_view_search_attribute_groupby +msgid "Attribute Value 1" +msgstr "" + +#. module: product_attribute_groupby_filter +#: model_terms:ir.ui.view,arch_db:product_attribute_groupby_filter.product_product_view_search_attribute_groupby +msgid "Attribute Value 2" +msgstr "" + +#. module: product_attribute_groupby_filter +#: model_terms:ir.ui.view,arch_db:product_attribute_groupby_filter.product_product_view_search_attribute_groupby +msgid "Attribute Value 3" +msgstr "" + +#. module: product_attribute_groupby_filter +#: model_terms:ir.ui.view,arch_db:product_attribute_groupby_filter.res_config_settings_view_form_groupby +msgid "Attribute values sort order" +msgstr "Ordre de Tri des Valeurs de Caractéristique" + +#. module: product_attribute_groupby_filter +#: model:ir.model,name:product_attribute_groupby_filter.model_res_company +msgid "Companies" +msgstr "Sociétés" + +#. module: product_attribute_groupby_filter +#: model:ir.model,name:product_attribute_groupby_filter.model_res_config_settings +msgid "Config Settings" +msgstr "Paramètres de configuration" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,help:product_attribute_groupby_filter.field_res_config_settings__product_groupby_attribute_sort +msgid "" +"Defines the sort order applied to attribute values when grouping products " +"by attribute in the searchbar.\n" +"- Sequence: values are sorted by their sequence field.\n" +"- Name: values are sorted alphabetically." +msgstr "" +"Définit l'ordre de tri appliqué aux valeurs d'attribut lors du regroupement " +"des produits par attribut dans la barre de recherche.\n" +" - Séquence : les valeurs sont triées en fonction de leur séquence. \n" +"- Nom : les valeurs sont triées par ordre alphabétique." + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_product_attribute__display_name +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_product_product__display_name +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_res_company__display_name +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_res_config_settings__display_name +msgid "Display Name" +msgstr "Nom affiché" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_product_attribute__exclude_from_groupby +msgid "Exclude from Group By" +msgstr "Exclure du filtre regrouper par Caractéristique" + +#. module: product_attribute_groupby_filter +#. openerp-web +#: code:addons/product_attribute_groupby_filter/static/src/xml/product_attribute_groupby.xml:0 +#, python-format +msgid "Group by Attribute" +msgstr "Regrouper par Caractéristique" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_product_attribute__id +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_product_product__id +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_res_company__id +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_res_config_settings__id +msgid "ID" +msgstr "Identifiant" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,help:product_attribute_groupby_filter.field_product_attribute__exclude_from_groupby +msgid "" +"If checked, this attribute will not appear in the 'Group by attribute' " +"search menu." +msgstr "" +"Si coché, cette caractéristique n'apparait pas dans le filtre regrouper par " +"Caractéristique." + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_product_attribute____last_update +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_product_product____last_update +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_res_company____last_update +#: model:ir.model.fields,field_description:product_attribute_groupby_filter.field_res_config_settings____last_update +msgid "Last Modified on" +msgstr "Dernière modification le" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields.selection,name:product_attribute_groupby_filter.selection__res_company__product_groupby_attribute_sort__name +msgid "Name" +msgstr "" + +#. module: product_attribute_groupby_filter +#: model:ir.model,name:product_attribute_groupby_filter.model_product_product +msgid "Product" +msgstr "Article" + +#. module: product_attribute_groupby_filter +#: model:ir.model,name:product_attribute_groupby_filter.model_product_attribute +msgid "Product Attribute" +msgstr "Caractéristique de l'article" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields.selection,name:product_attribute_groupby_filter.selection__res_company__product_groupby_attribute_sort__sequence +msgid "Sequence" +msgstr "" + +#. module: product_attribute_groupby_filter +#: model_terms:ir.ui.view,arch_db:product_attribute_groupby_filter.res_config_settings_view_form_groupby +msgid "" +"Sort order applied to attribute values when\n" +" grouping products by attribute in the searchbar." +msgstr "" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,help:product_attribute_groupby_filter.field_product_product__attribute_group_by_1 +msgid "Technical field for attribute groupBy slot 1." +msgstr "" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,help:product_attribute_groupby_filter.field_product_product__attribute_group_by_2 +msgid "Technical field for attribute groupBy slot 2." +msgstr "" + +#. module: product_attribute_groupby_filter +#: model:ir.model.fields,help:product_attribute_groupby_filter.field_product_product__attribute_group_by_3 +msgid "Technical field for attribute groupBy slot 3." +msgstr "" diff --git a/product_attribute_groupby_filter/models/__init__.py b/product_attribute_groupby_filter/models/__init__.py new file mode 100644 index 00000000000..4613671285d --- /dev/null +++ b/product_attribute_groupby_filter/models/__init__.py @@ -0,0 +1,4 @@ +from . import product_attribute +from . import product_product +from . import res_company +from . import res_config_settings diff --git a/product_attribute_groupby_filter/models/product_attribute.py b/product_attribute_groupby_filter/models/product_attribute.py new file mode 100644 index 00000000000..3182c70f47c --- /dev/null +++ b/product_attribute_groupby_filter/models/product_attribute.py @@ -0,0 +1,16 @@ +# Copyright 2026 Akretion (https://www.akretion.com). +# @author Kévin Roche +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ProductAttribute(models.Model): + _inherit = "product.attribute" + + exclude_from_groupby = fields.Boolean( + string="Exclude from Group By", + default=False, + help="If checked, this attribute will not appear in the " + " 'Group by attribute' search menu.", + ) diff --git a/product_attribute_groupby_filter/models/product_product.py b/product_attribute_groupby_filter/models/product_product.py new file mode 100644 index 00000000000..edbf6fc67c1 --- /dev/null +++ b/product_attribute_groupby_filter/models/product_product.py @@ -0,0 +1,226 @@ +# Copyright (C) 2026 Akretion (). +# @author Kévin Roche +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + +MAX_ATTRIBUTE_GROUPBY = 3 + + +class ProductProduct(models.Model): + _inherit = "product.product" + + attribute_group_by_1 = fields.Char( + string="Attribute Group By 1", + store=False, + help="Technical field for attribute groupBy slot 1", + ) + attribute_group_by_2 = fields.Char( + string="Attribute Group By 2", + store=False, + help="Technical field for attribute groupBy slot 2", + ) + attribute_group_by_3 = fields.Char( + string="Attribute Group By 3", + store=False, + help="Technical field for attribute groupBy slot 3", + ) + + @api.model + def get_groupby_attribute_fields(self): + attributes = self.env["product.attribute"].search( + [("exclude_from_groupby", "=", False)], + order="name", + ) + return [{"id": attr.id, "label": attr.name} for attr in attributes] + + @api.model + def _resolve_attribute_id(self, slot, domain): + context_key = "groupby_attribute_id_%d" % slot + attribute_id = self.env.context.get(context_key) + if attribute_id: + return attribute_id + + all_requested = [ + self.env.context.get("groupby_attribute_id_%d" % s) + for s in range(1, MAX_ATTRIBUTE_GROUPBY + 1) + if self.env.context.get("groupby_attribute_id_%d" % s) + ] + if not all_requested: + return None + + already_used = set() + for leaf in domain: + if ( + isinstance(leaf, (list, tuple)) + and len(leaf) == 3 + and leaf[0] == "product_template_attribute_value_ids" + and leaf[1] == "in" + and leaf[2] + ): + ptavs = self.env["product.template.attribute.value"].browse(leaf[2]) + for ptav in ptavs: + already_used.add(ptav.attribute_id.id) + + return next((aid for aid in all_requested if aid not in already_used), None) + + @api.model + def _build_pav_map(self, attribute_id, template_ids): + ptavs = self.env["product.template.attribute.value"].search( + [ + ("attribute_id", "=", attribute_id), + ("product_tmpl_id", "in", template_ids), + ], + order="product_attribute_value_id", + ) + + pav_map = {} + for ptav in ptavs: + pav = ptav.product_attribute_value_id + if pav.id not in pav_map: + pav_map[pav.id] = { + "pav": pav, + "ptav_ids": [], + "sequence": pav.sequence, + } + pav_map[pav.id]["ptav_ids"].append(ptav.id) + + return pav_map + + @api.model + def _build_attribute_groups(self, field_name, domain, pav_map, remaining_groupby): + def _sub_context(remaining_groupby): + ctx = {"group_by": remaining_groupby} + for s in range(1, MAX_ATTRIBUTE_GROUPBY + 1): + val = self.env.context.get("groupby_attribute_id_%d" % s) + if val: + ctx["groupby_attribute_id_%d" % s] = val + return ctx + + res = [] + for data in pav_map.values(): + value_domain = list(domain) + [ + ("product_template_attribute_value_ids", "in", data["ptav_ids"]) + ] + count = self.search_count(value_domain) + if count == 0: + continue + res.append( + { + field_name: (data["pav"].id, data["pav"].name), + "%s_count" % field_name: count, + "__domain": value_domain, + "__context": _sub_context(remaining_groupby), + "__fold": False, + "_sequence": data["sequence"], + } + ) + return res + + @api.model + def _append_undefined_group( + self, res, field_name, domain, pav_map, remaining_groupby + ): + all_ptav_ids = [ + ptav_id for data in pav_map.values() for ptav_id in data["ptav_ids"] + ] + undefined_domain = list(domain) + [ + ("product_template_attribute_value_ids", "not in", all_ptav_ids) + ] + undefined_count = self.search_count(undefined_domain) + if undefined_count == 0: + return + + sub_context = {"group_by": remaining_groupby} + for s in range(1, MAX_ATTRIBUTE_GROUPBY + 1): + val = self.env.context.get("groupby_attribute_id_%d" % s) + if val: + sub_context["groupby_attribute_id_%d" % s] = val + + res.append( + { + field_name: (False, "Undefined"), + "%s_count" % field_name: undefined_count, + "__domain": undefined_domain, + "__context": sub_context, + "__fold": False, + "_sequence": 9999, + } + ) + + @api.model + def _sort_and_clean_groups(self, res, field_name, sort_order): + if sort_order == "sequence": + res.sort(key=lambda r: (r["_sequence"], r[field_name][1] or "")) + else: + res.sort(key=lambda r: r[field_name][1] or "") + for group in res: + del group["_sequence"] + + @api.model + def _get_attribute_groups(self, slot, domain, groupby_list): + field_name = "attribute_group_by_%d" % slot + + if field_name not in groupby_list: + return None + + attribute_id = self._resolve_attribute_id(slot, domain) + if not attribute_id: + return None + + template_ids = self.search(domain).mapped("product_tmpl_id").ids + if not template_ids: + return [] + + remaining_groupby = [g for g in groupby_list if g != field_name] + pav_map = self._build_pav_map(attribute_id, template_ids) + + res = self._build_attribute_groups( + field_name, domain, pav_map, remaining_groupby + ) + self._append_undefined_group( + res, field_name, domain, pav_map, remaining_groupby + ) + + sort_order = self.env.company.product_groupby_attribute_sort or "sequence" + self._sort_and_clean_groups(res, field_name, sort_order) + + return res + + @api.model + def read_group( + self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True + ): + groupby_list = groupby if isinstance(groupby, list) else [groupby] + fields_to_check = groupby_list[:1] if lazy else groupby_list + for field_name in fields_to_check: + for slot in range(1, MAX_ATTRIBUTE_GROUPBY + 1): + if field_name == "attribute_group_by_%d" % slot: + result = self._get_attribute_groups(slot, domain, groupby_list) + if result is not None: + return result + break + + result = super().read_group( + domain, + fields, + groupby, + offset=offset, + limit=limit, + orderby=orderby, + lazy=lazy, + ) + + attr_ctx = { + "groupby_attribute_id_%d" + % i: self.env.context.get("groupby_attribute_id_%d" % i) + for i in range(1, MAX_ATTRIBUTE_GROUPBY + 1) + if self.env.context.get("groupby_attribute_id_%d" % i) + } + if attr_ctx: + for group in result: + group_ctx = dict(group.get("__context") or {}) + group_ctx.update(attr_ctx) + group["__context"] = group_ctx + + return result diff --git a/product_attribute_groupby_filter/models/res_company.py b/product_attribute_groupby_filter/models/res_company.py new file mode 100644 index 00000000000..916985437a5 --- /dev/null +++ b/product_attribute_groupby_filter/models/res_company.py @@ -0,0 +1,19 @@ +# Copyright 2026 Akretion (https://www.akretion.com). +# @author Kévin Roche +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResCompany(models.Model): + _inherit = "res.company" + + product_groupby_attribute_sort = fields.Selection( + selection=[ + ("sequence", "Sequence"), + ("name", "Name"), + ], + string="Attribute Group By Sort Order", + default="sequence", + required=True, + ) diff --git a/product_attribute_groupby_filter/models/res_config_settings.py b/product_attribute_groupby_filter/models/res_config_settings.py new file mode 100644 index 00000000000..8017b458012 --- /dev/null +++ b/product_attribute_groupby_filter/models/res_config_settings.py @@ -0,0 +1,19 @@ +# Copyright 2026 Akretion (https://www.akretion.com). +# @author Kévin Roche +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + product_groupby_attribute_sort = fields.Selection( + related="company_id.product_groupby_attribute_sort", + string="Attribute Group By Sort Order", + readonly=False, + help="Defines the sort order applied to attribute values " + "when grouping products by attribute in the searchbar.\n" + "- Sequence: values are sorted by their sequence field.\n" + "- Name: values are sorted alphabetically.", + ) diff --git a/product_attribute_groupby_filter/readme/CONTRIBUTORS.rst b/product_attribute_groupby_filter/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..dcae277c8c6 --- /dev/null +++ b/product_attribute_groupby_filter/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Kévin Roche diff --git a/product_attribute_groupby_filter/readme/DESCRIPTION.rst b/product_attribute_groupby_filter/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..3c4d1d920ed --- /dev/null +++ b/product_attribute_groupby_filter/readme/DESCRIPTION.rst @@ -0,0 +1,7 @@ +This module adds a **"Group by Attribute"** option to the searchbar of the +``product.product`` list view. It allows users to dynamically group product +variants by any attribute value (e.g. Size → S / M / L, Color → Red / Blue) +directly from the Group By menu, without requiring dedicated groupby fields for +each attribute. + +Up to **3 simultaneous attribute groupings** are supported. diff --git a/product_attribute_groupby_filter/readme/USAGE.rst b/product_attribute_groupby_filter/readme/USAGE.rst new file mode 100644 index 00000000000..cb439757484 --- /dev/null +++ b/product_attribute_groupby_filter/readme/USAGE.rst @@ -0,0 +1,8 @@ +* **Dynamic Group By**: group ``product.product`` records by any + ``product.attribute`` value from the searchbar Group By menu. +* **Multi-level grouping**: combine up to 3 attribute groupings, optionally + combined with native Odoo groupBy fields. +* **Sort order**: attribute values can be sorted by *sequence* (default) or + *alphabetically*, configurable in settings. +* **Exclude from Group By**: individual attributes can be excluded from the + widget via a dedicated flag on the attribute form. diff --git a/product_attribute_groupby_filter/static/description/index.html b/product_attribute_groupby_filter/static/description/index.html new file mode 100644 index 00000000000..838d5a24fed --- /dev/null +++ b/product_attribute_groupby_filter/static/description/index.html @@ -0,0 +1,444 @@ + + + + + +Product Attribute Groupby Filter + + + +
+

Product Attribute Groupby Filter

+ + +

Beta License: AGPL-3 OCA/product-attribute Translate me on Weblate Try me on Runboat

+

This module adds a “Group by Attribute” option to the searchbar of the +product.product list view. It allows users to dynamically group product +variants by any attribute value (e.g. Size → S / M / L, Color → Red / Blue) +directly from the Group By menu, without requiring dedicated groupby fields for +each attribute.

+

Up to 3 simultaneous attribute groupings are supported.

+

Table of contents

+ +
+

Usage

+
    +
  • Dynamic Group By: group product.product records by any +product.attribute value from the searchbar Group By menu.
  • +
  • Multi-level grouping: combine up to 3 attribute groupings, optionally +combined with native Odoo groupBy fields.
  • +
  • Sort order: attribute values can be sorted by sequence (default) or +alphabetically, configurable in settings.
  • +
  • Exclude from Group By: individual attributes can be excluded from the +widget via a dedicated flag on the attribute form.
  • +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Akretion
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

Kev-Roche

+

This module is part of the OCA/product-attribute project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/product_attribute_groupby_filter/static/src/js/control_panel_model.js b/product_attribute_groupby_filter/static/src/js/control_panel_model.js new file mode 100644 index 00000000000..cda8dfc149a --- /dev/null +++ b/product_attribute_groupby_filter/static/src/js/control_panel_model.js @@ -0,0 +1,29 @@ +odoo.define("product_attribute_groupby.PatchFavoriteItem", function (require) { + "use strict"; + + const ControlPanelModelExtension = require("web/static/src/js/control_panel/control_panel_model_extension.js"); + + const originalCreateNewFavorite = + ControlPanelModelExtension.prototype.createNewFavorite; + + ControlPanelModelExtension.prototype.createNewFavorite = async function ( + preFilter + ) { + const attrIds = this._pagAttributeIds || {}; + + if (!Object.keys(attrIds).length) { + return originalCreateNewFavorite.call(this, preFilter); + } + + const originalUserContext = Object.assign({}, this.env.session.user_context); + Object.assign(this.env.session.user_context, attrIds); + try { + return await originalCreateNewFavorite.call(this, preFilter); + } finally { + for (const key of Object.keys(attrIds)) { + delete this.env.session.user_context[key]; + } + Object.assign(this.env.session.user_context, originalUserContext); + } + }; +}); diff --git a/product_attribute_groupby_filter/static/src/js/product_attribute_groupby.js b/product_attribute_groupby_filter/static/src/js/product_attribute_groupby.js new file mode 100644 index 00000000000..16de27fc14c --- /dev/null +++ b/product_attribute_groupby_filter/static/src/js/product_attribute_groupby.js @@ -0,0 +1,146 @@ +odoo.define("product_attribute_groupby.ProductAttributeGroupBy", function (require) { + "use strict"; + const DropdownMenuItem = require("web.DropdownMenuItem"); + const GroupByMenu = require("web.GroupByMenu"); + const patchMixin = require("web.patchMixin"); + const {useModel} = require("web/static/src/js/model.js"); + const {onWillStart, useState} = owl.hooks; + const MAX_SLOTS = 3; + + class ProductAttributeGroupBy extends DropdownMenuItem { + constructor() { + super(...arguments); + this.model = useModel("searchModel"); + this._resModel = this.model.config.modelName; + } + setup() { + super.setup(); + this.state = useState({open: false, attributes: [], selectedId: null}); + onWillStart(async () => { + if (this._resModel !== "product.product") return; + try { + const attrs = await this.env.services.rpc({ + model: "product.product", + method: "get_groupby_attribute_fields", + args: [], + kwargs: {context: {}}, + }); + this.state.attributes = attrs; + if (attrs.length) this.state.selectedId = attrs[0].id; + } catch (e) { + console.error("[ProductAttributeGroupBy] RPC error:", e); + } + }); + } + _onChangeSelect(ev) { + this.state.selectedId = parseInt(ev.target.value, 10); + } + + _activeSlotFor(attrId) { + const filters = this.model.get("filters") || []; + for (let slot = 1; slot <= MAX_SLOTS; slot++) { + const pag = filters.find( + (f) => + f.type === "filter" && + f.description === "__groupby_attr_" + slot + ); + if (!pag) continue; + const ctx = pag.context || {}; + if (ctx["groupby_attribute_id_" + slot] === attrId) return slot; + } + return null; + } + + _nextFreeSlot() { + const filters = this.model.get("filters") || []; + for (let slot = 1; slot <= MAX_SLOTS; slot++) { + const pag = filters.find( + (f) => + f.type === "filter" && + f.description === "__groupby_attr_" + slot + ); + if (!pag) return slot; + } + return null; + } + + _removeSlot(slot) { + const filters = this.model.get("filters") || []; + const groupBy = filters.find( + (f) => f.type === "groupBy" && f.name === "attribute_group_by_" + slot + ); + if (groupBy) this.model.dispatch("deleteSearchItem", groupBy.id); + const pag = filters.find( + (f) => f.type === "filter" && f.description === "__groupby_attr_" + slot + ); + if (pag) this.model.dispatch("deleteSearchItem", pag.id); + const ctx = Object.assign({}, this.model.config.context); + delete ctx["groupby_attribute_id_" + slot]; + this.model.config.context = ctx; + } + + _onApply() { + const selected = this.state.attributes.find( + (a) => a.id === this.state.selectedId + ); + if (!selected) return; + + const activeSlot = this._activeSlotFor(selected.id); + if (activeSlot !== null) { + this._removeSlot(activeSlot); + this.state.open = false; + return; + } + + const slot = this._nextFreeSlot(); + if (slot === null) return; + + const fieldName = "attribute_group_by_" + slot; + const ctxKey = "groupby_attribute_id_" + slot; + const field = this.model.config.fields[fieldName]; + if (!field) return; + + this.model.config.context = Object.assign({}, this.model.config.context, { + [ctxKey]: selected.id, + }); + this.model.dispatch("createNewFilters", [ + { + type: "filter", + description: "__groupby_attr_" + slot, + domain: "[]", + context: {["groupby_attribute_id_" + slot]: selected.id}, + }, + ]); + this.model.dispatch("createNewGroupBy", { + ...field, + description: selected.label, + name: fieldName, + string: selected.label, + }); + + this.state.open = false; + } + } + ProductAttributeGroupBy.template = + "product_attribute_groupby.ProductAttributeGroupBy"; + const PatchedProductAttributeGroupBy = patchMixin(ProductAttributeGroupBy); + GroupByMenu.components = Object.assign({}, GroupByMenu.components, { + ProductAttributeGroupBy: PatchedProductAttributeGroupBy, + }); + return PatchedProductAttributeGroupBy; +}); + +odoo.define("product_attribute_groupby.PatchFacets", function (require) { + "use strict"; + + const ControlPanelModelExtension = require("web/static/src/js/control_panel/control_panel_model_extension.js"); + + const originalGetFacets = ControlPanelModelExtension.prototype._getFacets; + + ControlPanelModelExtension.prototype._getFacets = function () { + return originalGetFacets.call(this).filter((facet) => { + if (facet.type !== "filter") return true; + return !facet.title.startsWith("__groupby_attr_"); + }); + }; +}); diff --git a/product_attribute_groupby_filter/static/src/xml/product_attribute_groupby.xml b/product_attribute_groupby_filter/static/src/xml/product_attribute_groupby.xml new file mode 100644 index 00000000000..0aaae1d5c41 --- /dev/null +++ b/product_attribute_groupby_filter/static/src/xml/product_attribute_groupby.xml @@ -0,0 +1,60 @@ + + + + +
+ + + +
+
+ + + + +