diff --git a/website_sale_hide_empty_category/README.rst b/website_sale_hide_empty_category/README.rst new file mode 100644 index 0000000000..9cd2f0498e --- /dev/null +++ b/website_sale_hide_empty_category/README.rst @@ -0,0 +1,110 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +==================================== +Website Sale - Hide Empty Categories +==================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:72c53172beae1416c2f8cfb67668bcbfba5dbff39310b0c6d8c9df8d54ca7c46 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/license-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fe--commerce-lightgray.png?logo=github + :target: https://github.com/OCA/e-commerce/tree/19.0/website_sale_hide_empty_category + :alt: OCA/e-commerce +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/e-commerce-19-0/e-commerce-19-0-website_sale_hide_empty_category + :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/e-commerce&target_branch=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Removes any category links from the Shop which have no products +associated to them. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +When the module is installed, by default the options to hide empty +categories are active. + +These options can be configured in the web editor. To do this, go to +‘/shop’ and activate or deactivate the options to hide empty categories +from the web editor in the ‘Customize’ tab. + +There are two options: + +- Hide Empty Category Filmstrip: to hide/show empty categories in the + top category bar. +- Hide Empty Category list: to hide/show empty categories in the left + category list. + +Usage +===== + +Once this module is installed and configured, go to ‘/shop’. Empty +categories will not be show. + +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 +------- + +* LasLabs + +Contributors +------------ + +- Ted Salmon +- `Tecnativa `__: + + - Carlos Roca + - Pilar Vargas + +- Radovan Skolnik + +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. + +This module is part of the `OCA/e-commerce `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/website_sale_hide_empty_category/__init__.py b/website_sale_hide_empty_category/__init__.py new file mode 100644 index 0000000000..1ada0880cc --- /dev/null +++ b/website_sale_hide_empty_category/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2017 LasLabs Inc. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). +from . import models diff --git a/website_sale_hide_empty_category/__manifest__.py b/website_sale_hide_empty_category/__manifest__.py new file mode 100644 index 0000000000..a7f4fd8272 --- /dev/null +++ b/website_sale_hide_empty_category/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2017 LasLabs Inc. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). +{ + "name": "Website Sale - Hide Empty Categories", + "summary": "Hide any Product Categories that are empty", + "version": "19.0.1.0.0", + "category": "Website", + "website": "https://github.com/OCA/e-commerce", + "author": "LasLabs, Odoo Community Association (OCA)", + "license": "LGPL-3", + "application": False, + "installable": True, + "depends": ["website_sale"], + "data": ["views/website_sale_templates.xml"], + "assets": { + "web.assets_tests": [ + "/website_sale_hide_empty_category/static/src/js/tour.esm.js" + ], + }, +} diff --git a/website_sale_hide_empty_category/i18n/ca.po b/website_sale_hide_empty_category/i18n/ca.po new file mode 100644 index 0000000000..67fee65b06 --- /dev/null +++ b/website_sale_hide_empty_category/i18n/ca.po @@ -0,0 +1,27 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * website_sale_hide_empty_category +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2026-02-25 07:27+0000\n" +"Last-Translator: Anonymous \n" +"Language-Team: none\n" +"Language: ca\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 5.15.2\n" + +#. module: website_sale_hide_empty_category +#: model:ir.model.fields,field_description:website_sale_hide_empty_category.field_product_public_category__has_product_recursive +msgid "This category or one of its children has products" +msgstr "Aquesta categoria o un dels seus fills té productes" + +#. module: website_sale_hide_empty_category +#: model:ir.model,name:website_sale_hide_empty_category.model_product_public_category +msgid "Website Product Category" +msgstr "Categoria de producte del lloc web" diff --git a/website_sale_hide_empty_category/i18n/es.po b/website_sale_hide_empty_category/i18n/es.po new file mode 100644 index 0000000000..9978d11ac6 --- /dev/null +++ b/website_sale_hide_empty_category/i18n/es.po @@ -0,0 +1,31 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * website_sale_hide_empty_category +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: \n" +"PO-Revision-Date: 2026-02-25 07:28+0000\n" +"Last-Translator: Anonymous \n" +"Language-Team: \n" +"Language: es\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: Weblate 5.15.2\n" + +#. module: website_sale_hide_empty_category +#: model:ir.model.fields,field_description:website_sale_hide_empty_category.field_product_public_category__has_product_recursive +msgid "This category or one of its children has products" +msgstr "Aquesta categoria o un dels seus fills té productes" + +#. module: website_sale_hide_empty_category +#: model:ir.model,name:website_sale_hide_empty_category.model_product_public_category +msgid "Website Product Category" +msgstr "Categoría del Producto" + +#~ msgid "Products" +#~ msgstr "Productos" diff --git a/website_sale_hide_empty_category/i18n/it.po b/website_sale_hide_empty_category/i18n/it.po new file mode 100644 index 0000000000..1ee7c71716 --- /dev/null +++ b/website_sale_hide_empty_category/i18n/it.po @@ -0,0 +1,27 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * website_sale_hide_empty_category +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2026-02-25 07:30+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 5.15.2\n" + +#. module: website_sale_hide_empty_category +#: model:ir.model.fields,field_description:website_sale_hide_empty_category.field_product_public_category__has_product_recursive +msgid "This category or one of its children has products" +msgstr "Questa categoria o una delle sue figlie contiene prodotti" + +#. module: website_sale_hide_empty_category +#: model:ir.model,name:website_sale_hide_empty_category.model_product_public_category +msgid "Website Product Category" +msgstr "Categoria prodotto sito web" diff --git a/website_sale_hide_empty_category/i18n/nl.po b/website_sale_hide_empty_category/i18n/nl.po new file mode 100644 index 0000000000..7eda286066 --- /dev/null +++ b/website_sale_hide_empty_category/i18n/nl.po @@ -0,0 +1,27 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * website_sale_hide_empty_category +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2026-02-25 07:29+0000\n" +"Last-Translator: Anonymous \n" +"Language-Team: none\n" +"Language: nl\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 5.15.2\n" + +#. module: website_sale_hide_empty_category +#: model:ir.model.fields,field_description:website_sale_hide_empty_category.field_product_public_category__has_product_recursive +msgid "This category or one of its children has products" +msgstr "Deze categorie of een van de subcategorieen bevat geen producten" + +#. module: website_sale_hide_empty_category +#: model:ir.model,name:website_sale_hide_empty_category.model_product_public_category +msgid "Website Product Category" +msgstr "Website Product Categorie" diff --git a/website_sale_hide_empty_category/i18n/pt_BR.po b/website_sale_hide_empty_category/i18n/pt_BR.po new file mode 100644 index 0000000000..0f4c9f2a8c --- /dev/null +++ b/website_sale_hide_empty_category/i18n/pt_BR.po @@ -0,0 +1,27 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * website_sale_hide_empty_category +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2026-02-25 07:30+0000\n" +"Last-Translator: Anonymous \n" +"Language-Team: none\n" +"Language: pt_BR\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n > 1;\n" +"X-Generator: Weblate 5.15.2\n" + +#. module: website_sale_hide_empty_category +#: model:ir.model.fields,field_description:website_sale_hide_empty_category.field_product_public_category__has_product_recursive +msgid "This category or one of its children has products" +msgstr "" + +#. module: website_sale_hide_empty_category +#: model:ir.model,name:website_sale_hide_empty_category.model_product_public_category +msgid "Website Product Category" +msgstr "" diff --git a/website_sale_hide_empty_category/i18n/sl.po b/website_sale_hide_empty_category/i18n/sl.po new file mode 100644 index 0000000000..f55bf6721f --- /dev/null +++ b/website_sale_hide_empty_category/i18n/sl.po @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * website_sale_hide_empty_category +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2026-06-27 07:46+0000\n" +"Last-Translator: Matjaz Mozetic \n" +"Language-Team: none\n" +"Language: sl\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=4; plural=n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || " +"n%100==4 ? 2 : 3;\n" +"X-Generator: Weblate 5.15.2\n" + +#. module: website_sale_hide_empty_category +#: model:ir.model.fields,field_description:website_sale_hide_empty_category.field_product_public_category__has_product_recursive +msgid "This category or one of its children has products" +msgstr "Ta kategorija ali ena od njenih podkategorij vsebuje proizvode" + +#. module: website_sale_hide_empty_category +#: model:ir.model,name:website_sale_hide_empty_category.model_product_public_category +msgid "Website Product Category" +msgstr "Spletna kategorija proizvoda" diff --git a/website_sale_hide_empty_category/i18n/website_sale_hide_empty_category.pot b/website_sale_hide_empty_category/i18n/website_sale_hide_empty_category.pot new file mode 100644 index 0000000000..252543be60 --- /dev/null +++ b/website_sale_hide_empty_category/i18n/website_sale_hide_empty_category.pot @@ -0,0 +1,24 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * website_sale_hide_empty_category +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: website_sale_hide_empty_category +#: model:ir.model.fields,field_description:website_sale_hide_empty_category.field_product_public_category__has_product_recursive +msgid "This category or one of its children has products" +msgstr "" + +#. module: website_sale_hide_empty_category +#: model:ir.model,name:website_sale_hide_empty_category.model_product_public_category +msgid "Website Product Category" +msgstr "" diff --git a/website_sale_hide_empty_category/models/__init__.py b/website_sale_hide_empty_category/models/__init__.py new file mode 100644 index 0000000000..61f94bce68 --- /dev/null +++ b/website_sale_hide_empty_category/models/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2017 LasLabs Inc. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). + +from . import product_public_category +from . import website_snippet_filter diff --git a/website_sale_hide_empty_category/models/product_public_category.py b/website_sale_hide_empty_category/models/product_public_category.py new file mode 100644 index 0000000000..f6de8f3c60 --- /dev/null +++ b/website_sale_hide_empty_category/models/product_public_category.py @@ -0,0 +1,30 @@ +# Copyright 2017 LasLabs Inc. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). + +from odoo import api, fields, models +from odoo.fields import Domain + + +class ProductPublicCategory(models.Model): + _inherit = "product.public.category" + + has_product_recursive = fields.Boolean( + string="This category or one of its children has products", + compute="_compute_has_product_recursive", + recursive=True, + ) + + @api.depends("product_tmpl_ids", "child_id.has_product_recursive") + @api.depends_context("website_id") + def _compute_has_product_recursive(self): + for category in self: + website = self.env["website"].get_current_website() + website_domain = ( + Domain([("is_published", "=", True)]) & website.sale_product_domain() + ) + has_products = bool( + category.product_tmpl_ids.filtered_domain(website_domain) + ) + category.has_product_recursive = has_products or any( + child.has_product_recursive for child in category.child_id + ) diff --git a/website_sale_hide_empty_category/models/website_snippet_filter.py b/website_sale_hide_empty_category/models/website_snippet_filter.py new file mode 100644 index 0000000000..1d3da57002 --- /dev/null +++ b/website_sale_hide_empty_category/models/website_snippet_filter.py @@ -0,0 +1,38 @@ +# Copyright 2017 LasLabs Inc. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). + +from odoo import api, models +from odoo.http import request + + +class WebsiteSnippetFilter(models.Model): + _inherit = "website.snippet.filter" + + @api.model + def _prepare_category_list_data(self, parent_id=None): + """Extend category list data to include has_product_recursive. + + The dynamic category snippet renders each category from a plain dict, + not an ORM recordset. The `has_product_recursive` field defined on + `product.public.category` is therefore unavailable in the template + unless explicitly added here. + + This allows the snippet template to filter out empty categories + consistently with the sidebar/filmstrip behaviour which uses + `c.has_product_recursive` directly on ORM records. + """ + result = super()._prepare_category_list_data(parent_id=parent_id) + if not result: + return result + + cat_ids = [cat_data["id"] for cat_data in result] + env = request.env if request else self.env + categories = env["product.public.category"].sudo().browse(cat_ids) + cat_map = {cat.id: cat for cat in categories} + + for cat_data in result: + cat = cat_map.get(cat_data["id"]) + cat_data["has_product_recursive"] = ( + cat.has_product_recursive if cat else False + ) + return result diff --git a/website_sale_hide_empty_category/pyproject.toml b/website_sale_hide_empty_category/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/website_sale_hide_empty_category/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/website_sale_hide_empty_category/readme/CONFIGURE.md b/website_sale_hide_empty_category/readme/CONFIGURE.md new file mode 100644 index 0000000000..3460499f8d --- /dev/null +++ b/website_sale_hide_empty_category/readme/CONFIGURE.md @@ -0,0 +1,9 @@ +When the module is installed, by default the options to hide empty categories are active. + +These options can be configured in the web editor. To do this, go to ‘/shop’ and activate +or deactivate the options to hide empty categories from the web editor in the ‘Customize’ tab. + +There are two options: + +- Hide Empty Category Filmstrip: to hide/show empty categories in the top category bar. +- Hide Empty Category list: to hide/show empty categories in the left category list. diff --git a/website_sale_hide_empty_category/readme/CONTRIBUTORS.md b/website_sale_hide_empty_category/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..894791435b --- /dev/null +++ b/website_sale_hide_empty_category/readme/CONTRIBUTORS.md @@ -0,0 +1,5 @@ +- Ted Salmon \<\> +- [Tecnativa](https://www.tecnativa.com): + - Carlos Roca + - Pilar Vargas +- Radovan Skolnik \<\> diff --git a/website_sale_hide_empty_category/readme/DESCRIPTION.md b/website_sale_hide_empty_category/readme/DESCRIPTION.md new file mode 100644 index 0000000000..67a00ead92 --- /dev/null +++ b/website_sale_hide_empty_category/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +Removes any category links from the Shop which have no products +associated to them. diff --git a/website_sale_hide_empty_category/readme/USAGE.md b/website_sale_hide_empty_category/readme/USAGE.md new file mode 100644 index 0000000000..c812fbf327 --- /dev/null +++ b/website_sale_hide_empty_category/readme/USAGE.md @@ -0,0 +1 @@ +Once this module is installed and configured, go to ‘/shop’. Empty categories will not be show. diff --git a/website_sale_hide_empty_category/static/description/icon.png b/website_sale_hide_empty_category/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/website_sale_hide_empty_category/static/description/icon.png differ diff --git a/website_sale_hide_empty_category/static/description/index.html b/website_sale_hide_empty_category/static/description/index.html new file mode 100644 index 0000000000..631f458b30 --- /dev/null +++ b/website_sale_hide_empty_category/static/description/index.html @@ -0,0 +1,458 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Website Sale - Hide Empty Categories

+ +

Beta License: LGPL-3 OCA/e-commerce Translate me on Weblate Try me on Runboat

+

Removes any category links from the Shop which have no products +associated to them.

+

Table of contents

+ +
+

Configuration

+

When the module is installed, by default the options to hide empty +categories are active.

+

These options can be configured in the web editor. To do this, go to +‘/shop’ and activate or deactivate the options to hide empty categories +from the web editor in the ‘Customize’ tab.

+

There are two options:

+
    +
  • Hide Empty Category Filmstrip: to hide/show empty categories in the +top category bar.
  • +
  • Hide Empty Category list: to hide/show empty categories in the left +category list.
  • +
+
+
+

Usage

+

Once this module is installed and configured, go to ‘/shop’. Empty +categories will not be show.

+
+
+

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

+
    +
  • LasLabs
  • +
+
+
+

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.

+

This module is part of the OCA/e-commerce project on GitHub.

+

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

+
+
+
+
+ + diff --git a/website_sale_hide_empty_category/static/src/js/tour.esm.js b/website_sale_hide_empty_category/static/src/js/tour.esm.js new file mode 100644 index 0000000000..ca373c8509 --- /dev/null +++ b/website_sale_hide_empty_category/static/src/js/tour.esm.js @@ -0,0 +1,25 @@ +/* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ +import {registry} from "@web/core/registry"; + +registry.category("web_tour.tours").add("website_sale_hide_empty_category", { + url: "/shop", + steps: () => [ + { + trigger: "#products_grid_before a:contains('Category Test Posted')", + }, + { + trigger: + "#products_grid_before:not(:has(a:contains('Category Test Not Posted')))", + }, + { + trigger: "a[href='/shop']", + }, + { + trigger: ".o_wsale_filmstrip_wrapper:contains('Category Test Posted')", + }, + { + trigger: + ".o_wsale_filmstrip_wrapper:not(:has(:contains('Category Test Not Posted')))", + }, + ], +}); diff --git a/website_sale_hide_empty_category/tests/__init__.py b/website_sale_hide_empty_category/tests/__init__.py new file mode 100644 index 0000000000..259b87b426 --- /dev/null +++ b/website_sale_hide_empty_category/tests/__init__.py @@ -0,0 +1,2 @@ +from . import test_ui +from . import test_has_product_recursive diff --git a/website_sale_hide_empty_category/tests/test_has_product_recursive.py b/website_sale_hide_empty_category/tests/test_has_product_recursive.py new file mode 100644 index 0000000000..ff72b4348b --- /dev/null +++ b/website_sale_hide_empty_category/tests/test_has_product_recursive.py @@ -0,0 +1,150 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from unittest.mock import patch + +from odoo.tests import common + +from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT +from odoo.addons.http_routing.tests.common import MockRequest + + +class TestPublicCategory(common.TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT)) + cls.website = cls.env["website"].get_current_website() + + def test_has_product_recursive(self): + category_1 = self.env["product.public.category"].create({"name": "Category 1"}) + self.assertFalse(category_1.has_product_recursive) + + category_2 = self.env["product.public.category"].create( + {"name": "Category 2", "parent_id": category_1.id} + ) + self.assertFalse(category_2.has_product_recursive) + + self.env["product.template"].create( + { + "name": "Product Test", + "is_published": True, + "public_categ_ids": [(4, category_2.id)], + } + ) + + self.assertTrue(category_1.has_product_recursive) + self.assertTrue(category_2.has_product_recursive) + + def test_has_product_recursive_unpublished(self): + category = self.env["product.public.category"].create( + {"name": "Category Unpublished"} + ) + self.assertFalse(category.has_product_recursive) + + self.env["product.template"].create( + { + "name": "Unpublished Product", + "is_published": False, + "public_categ_ids": [(4, category.id)], + } + ) + self.assertFalse(category.has_product_recursive) + + def test_has_product_recursive_multi_level(self): + cat_top = self.env["product.public.category"].create({"name": "Top Category"}) + cat_mid = self.env["product.public.category"].create( + {"name": "Mid Category", "parent_id": cat_top.id} + ) + cat_low = self.env["product.public.category"].create( + {"name": "Low Category", "parent_id": cat_mid.id} + ) + + self.assertFalse(cat_top.has_product_recursive) + self.assertFalse(cat_mid.has_product_recursive) + self.assertFalse(cat_low.has_product_recursive) + + self.env["product.template"].create( + { + "name": "Grandchild Product", + "is_published": True, + "public_categ_ids": [(4, cat_low.id)], + } + ) + + self.assertTrue(cat_top.has_product_recursive) + self.assertTrue(cat_mid.has_product_recursive) + self.assertTrue(cat_low.has_product_recursive) + + def test_prepare_category_list_data(self): + category_1 = self.env["product.public.category"].create( + {"name": "Category Snippet Published"} + ) + category_2 = self.env["product.public.category"].create( + {"name": "Category Snippet Empty"} + ) + + self.env["product.template"].create( + { + "name": "Published Snippet Product", + "is_published": True, + "public_categ_ids": [(4, category_1.id)], + } + ) + + snippet_filter = self.env["website.snippet.filter"] + with MockRequest(self.env, website=self.website): + res = snippet_filter._prepare_category_list_data() + + cat1_data = next((c for c in res if c["id"] == category_1.id), None) + cat2_data = next((c for c in res if c["id"] == category_2.id), None) + + self.assertIsNotNone(cat1_data) + self.assertTrue(cat1_data.get("has_product_recursive")) + + self.assertIsNotNone(cat2_data) + self.assertFalse(cat2_data.get("has_product_recursive")) + + def test_prepare_category_list_data_with_parent_id(self): + parent_cat = self.env["product.public.category"].create( + {"name": "Parent Category"} + ) + child_cat = self.env["product.public.category"].create( + {"name": "Child Category", "parent_id": parent_cat.id} + ) + + self.env["product.template"].create( + { + "name": "Child Product", + "is_published": True, + "public_categ_ids": [(4, child_cat.id)], + } + ) + + snippet_filter = self.env["website.snippet.filter"] + with MockRequest(self.env, website=self.website): + res = snippet_filter._prepare_category_list_data(parent_id=parent_cat.id) + + self.assertTrue(res) + child_data = next((c for c in res if c["id"] == child_cat.id), None) + self.assertIsNotNone(child_data) + self.assertTrue(child_data.get("has_product_recursive")) + + def test_prepare_category_list_data_empty(self): + empty_parent = self.env["product.public.category"].create( + {"name": "Empty Parent Category"} + ) + snippet_filter = self.env["website.snippet.filter"] + with MockRequest(self.env, website=self.website): + res = snippet_filter._prepare_category_list_data(parent_id=empty_parent.id) + self.assertTrue(res) + self.assertFalse(res[0]["has_product_recursive"]) + + def test_prepare_category_list_data_no_result(self): + snippet_filter = self.env["website.snippet.filter"] + with patch( + "odoo.addons.website_sale.models.website_snippet_filter.WebsiteSnippetFilter._prepare_category_list_data", + return_value=[], + ): + with MockRequest(self.env, website=self.website): + res = snippet_filter._prepare_category_list_data() + self.assertEqual(res, []) diff --git a/website_sale_hide_empty_category/tests/test_ui.py b/website_sale_hide_empty_category/tests/test_ui.py new file mode 100644 index 0000000000..e0fb4a29eb --- /dev/null +++ b/website_sale_hide_empty_category/tests/test_ui.py @@ -0,0 +1,33 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests.common import HttpCase, tagged + +from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT + + +@tagged("post_install", "-at_install") +class UICase(HttpCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT)) + category_posted = cls.env["product.public.category"].create( + {"name": "Category Test Posted", "sequence": 1} + ) + cls.env["product.public.category"].create( + {"name": "Category Test Not Posted", "sequence": 2} + ) + cls.env["product.template"].create( + { + "name": "Test Product 1", + "is_published": True, + "website_sequence": 1, + "type": "consu", + "public_categ_ids": [category_posted.id], + } + ) + cls.env.ref("website_sale.products_categories").active = True + + def test_ui_website(self): + """Test frontend tour.""" + self.start_tour("/shop", "website_sale_hide_empty_category", login=None) diff --git a/website_sale_hide_empty_category/views/website_sale_templates.xml b/website_sale_hide_empty_category/views/website_sale_templates.xml new file mode 100644 index 0000000000..be1315b117 --- /dev/null +++ b/website_sale_hide_empty_category/views/website_sale_templates.xml @@ -0,0 +1,76 @@ + + + + + + + + +