diff --git a/website_sale_product_document_variant/README.rst b/website_sale_product_document_variant/README.rst new file mode 100644 index 0000000000..fb2a18a77c --- /dev/null +++ b/website_sale_product_document_variant/README.rst @@ -0,0 +1,106 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +===================================== +Website Sale Product Document Variant +===================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:f9dd2a5cd1aebeaefe68c984e0130bad8e8991a75c4c032647f48e4fc03f6c96 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-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%2Fe--commerce-lightgray.png?logo=github + :target: https://github.com/OCA/e-commerce/tree/19.0/website_sale_product_document_variant + :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_product_document_variant + :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| + +Standard Odoo blocks publishing a product document that is attached to a +specific variant instead of the whole product: the "Publish on website" +option is hidden and cannot be enabled for it. + +This module lifts that limitation. A document attached to a variant can +be published, is shown on the product page alongside the product's own +documents for whichever variant the customer currently has selected, and +the list refreshes automatically -- without reloading the page -- when +the customer switches variant. The section disappears entirely when the +selected variant has no published document. Downloading such a document +is only allowed while it is published. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +1. On a product's variant (*Sales > Products > Variants*, or the + "Variants" smart button on a product), open the *Documents* tab (or + the linked *Attachments*) and enable "Publish on website" on the + document, exactly as already possible for a document attached to the + whole product. +2. On the eCommerce product page, the document appears in the + "Documents" section together with the product's own published + documents, for as long as that variant stays selected. Selecting a + different variant refreshes the list to that variant's own documents, + without a page reload. + +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 +------- + +* Camptocamp + +Contributors +------------ + +- `Camptocamp `__: + + - Ricardoalso + +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_product_document_variant/__init__.py b/website_sale_product_document_variant/__init__.py new file mode 100644 index 0000000000..91c5580fed --- /dev/null +++ b/website_sale_product_document_variant/__init__.py @@ -0,0 +1,2 @@ +from . import controllers +from . import models diff --git a/website_sale_product_document_variant/__manifest__.py b/website_sale_product_document_variant/__manifest__.py new file mode 100644 index 0000000000..e420bb27e4 --- /dev/null +++ b/website_sale_product_document_variant/__manifest__.py @@ -0,0 +1,26 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Website Sale Product Document Variant", + "summary": "Publish and refresh variant-specific documents on the product page", + "version": "19.0.1.0.0", + "development_status": "Beta", + "category": "Website", + "website": "https://github.com/OCA/e-commerce", + "author": "Camptocamp, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": [ + "website_sale", + ], + "data": [ + "views/product_document_views.xml", + "views/templates.xml", + ], + "assets": { + "web.assets_frontend": [ + "website_sale_product_document_variant/static/src/interactions/**/*", + ], + }, +} diff --git a/website_sale_product_document_variant/controllers/__init__.py b/website_sale_product_document_variant/controllers/__init__.py new file mode 100644 index 0000000000..57a00ae3c2 --- /dev/null +++ b/website_sale_product_document_variant/controllers/__init__.py @@ -0,0 +1,2 @@ +from . import main +from . import variant diff --git a/website_sale_product_document_variant/controllers/main.py b/website_sale_product_document_variant/controllers/main.py new file mode 100644 index 0000000000..4b9b6cc7dc --- /dev/null +++ b/website_sale_product_document_variant/controllers/main.py @@ -0,0 +1,45 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.http import request, route + +from odoo.addons.website_sale.controllers.main import WebsiteSale + + +class WebsiteSaleProductDocumentVariant(WebsiteSale): + @route( + '/shop//document/', + type="http", + auth="public", + website=True, + sitemap=False, + readonly=True, + ) + def product_document(self, product_template, document_id): + # Full override: the base method only accepts template documents + # (`res_model == 'product.template'`); this also accepts a + # document scoped to one of the template's own variants. + product_template.check_access("read") + + document = request.env["product.document"].browse(document_id).sudo().exists() + if not document or not document.active or not document.shown_on_product_page: + return request.redirect(self._get_shop_path()) + + is_template_document = ( + document.res_model == "product.template" + and document.res_id == product_template.id + ) + is_variant_document = ( + document.res_model == "product.product" + and document.res_id in product_template.product_variant_ids.ids + ) + if not (is_template_document or is_variant_document): + return request.redirect(self._get_shop_path()) + + return ( + request.env["ir.binary"] + ._get_stream_from( + document.ir_attachment_id, + ) + .get_response(as_attachment=True) + ) diff --git a/website_sale_product_document_variant/controllers/variant.py b/website_sale_product_document_variant/controllers/variant.py new file mode 100644 index 0000000000..1cdbecf585 --- /dev/null +++ b/website_sale_product_document_variant/controllers/variant.py @@ -0,0 +1,44 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.http import request, route + +from odoo.addons.website_sale.controllers.variant import WebsiteSaleVariantController + + +class WebsiteSaleProductDocumentVariantController(WebsiteSaleVariantController): + @route( + "/website_sale/get_combination_info", + type="jsonrpc", + auth="public", + methods=["POST"], + website=True, + readonly=True, + ) + def get_combination_info_website( + self, + product_template_id, + product_id, + combination, + add_qty, + uom_id=None, + **kwargs, + ): + combination_info = super().get_combination_info_website( + product_template_id=product_template_id, + product_id=product_id, + combination=combination, + add_qty=add_qty, + uom_id=uom_id, + **kwargs, + ) + product_template = request.env["product.template"].browse( + int(product_template_id) + ) + variant = request.env["product.product"].browse( + combination_info.get("product_id") + ) + combination_info["product_documents_html"] = ( + product_template._get_variant_documents_html(variant) + ) + return combination_info diff --git a/website_sale_product_document_variant/models/__init__.py b/website_sale_product_document_variant/models/__init__.py new file mode 100644 index 0000000000..5cb5807f32 --- /dev/null +++ b/website_sale_product_document_variant/models/__init__.py @@ -0,0 +1,2 @@ +from . import product_document +from . import product_template diff --git a/website_sale_product_document_variant/models/product_document.py b/website_sale_product_document_variant/models/product_document.py new file mode 100644 index 0000000000..9f09715446 --- /dev/null +++ b/website_sale_product_document_variant/models/product_document.py @@ -0,0 +1,16 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class ProductDocument(models.Model): + _inherit = "product.document" + + @api.constrains("res_model", "shown_on_product_page") + def _unsupported_product_product_document_on_ecommerce(self): + # Full override: this module makes the product page variant-aware, + # so the base method's blanket rejection of product.product + # documents no longer applies. + # Full override because the base loop unconditionally raises for that case. + return diff --git a/website_sale_product_document_variant/models/product_template.py b/website_sale_product_document_variant/models/product_template.py new file mode 100644 index 0000000000..ee4f3c6182 --- /dev/null +++ b/website_sale_product_document_variant/models/product_template.py @@ -0,0 +1,40 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class ProductTemplate(models.Model): + _inherit = "product.template" + + def _get_variant_documents(self, variant): + """Return the published documents for this template combined with + `variant`'s own published documents (`variant` may be empty). + """ + self.ensure_one() + documents = self.sudo().product_document_ids.filtered( + lambda doc: doc.shown_on_product_page + ) + if variant: + documents |= variant.sudo().product_document_ids.filtered( + lambda doc: doc.shown_on_product_page + ) + return documents + + def _get_variant_documents_html(self, variant): + """Render the documents section content for `variant` (a + `product.product` recordset, possibly empty), so it can be + refreshed client-side when the customer changes variant. + + Always renders (returns an empty section rather than `None` when + there is nothing to show) so the front-end can also hide the + section on refresh, not just skip updating it. + + Called only from the `/website_sale/get_combination_info` controller. + """ + self.ensure_one() + documents = self._get_variant_documents(variant) + return self.env["ir.qweb"]._render( + "website_sale_product_document_variant.product_documents_content", + {"product": self, "product_documents": documents}, + ) diff --git a/website_sale_product_document_variant/pyproject.toml b/website_sale_product_document_variant/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/website_sale_product_document_variant/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/website_sale_product_document_variant/readme/CONTRIBUTORS.md b/website_sale_product_document_variant/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..c27db30afb --- /dev/null +++ b/website_sale_product_document_variant/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Camptocamp](https://www.camptocamp.com): + - Ricardoalso \<\> diff --git a/website_sale_product_document_variant/readme/DESCRIPTION.md b/website_sale_product_document_variant/readme/DESCRIPTION.md new file mode 100644 index 0000000000..dacfceb181 --- /dev/null +++ b/website_sale_product_document_variant/readme/DESCRIPTION.md @@ -0,0 +1,11 @@ +Standard Odoo blocks publishing a product document that is attached to a +specific variant instead of the whole product: the "Publish on website" +option is hidden and cannot be enabled for it. + +This module lifts that limitation. A document attached to a variant can be +published, is shown on the product page alongside the product's own +documents for whichever variant the customer currently has selected, and +the list refreshes automatically -- without reloading the page -- when the +customer switches variant. The section disappears entirely when the +selected variant has no published document. Downloading such a document is +only allowed while it is published. diff --git a/website_sale_product_document_variant/readme/USAGE.md b/website_sale_product_document_variant/readme/USAGE.md new file mode 100644 index 0000000000..16cb940f72 --- /dev/null +++ b/website_sale_product_document_variant/readme/USAGE.md @@ -0,0 +1,10 @@ +1. On a product's variant (*Sales > Products > Variants*, or the + "Variants" smart button on a product), open the *Documents* tab (or + the linked *Attachments*) and enable "Publish on website" on the + document, exactly as already possible for a document attached to the + whole product. +2. On the eCommerce product page, the document appears in the + "Documents" section together with the product's own published + documents, for as long as that variant stays selected. Selecting a + different variant refreshes the list to that variant's own + documents, without a page reload. diff --git a/website_sale_product_document_variant/static/description/index.html b/website_sale_product_document_variant/static/description/index.html new file mode 100644 index 0000000000..f1b77a5b38 --- /dev/null +++ b/website_sale_product_document_variant/static/description/index.html @@ -0,0 +1,457 @@ + + + + + +Website Sale Product Document Variant + + + +
+ + + +Odoo Community Association + +
+

Website Sale Product Document Variant

+ +

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

+

Standard Odoo blocks publishing a product document that is attached to a +specific variant instead of the whole product: the “Publish on website” +option is hidden and cannot be enabled for it.

+

This module lifts that limitation. A document attached to a variant can +be published, is shown on the product page alongside the product’s own +documents for whichever variant the customer currently has selected, and +the list refreshes automatically – without reloading the page – when +the customer switches variant. The section disappears entirely when the +selected variant has no published document. Downloading such a document +is only allowed while it is published.

+

Table of contents

+ +
+

Usage

+
    +
  1. On a product’s variant (Sales > Products > Variants, or the +“Variants” smart button on a product), open the Documents tab (or +the linked Attachments) and enable “Publish on website” on the +document, exactly as already possible for a document attached to the +whole product.
  2. +
  3. On the eCommerce product page, the document appears in the +“Documents” section together with the product’s own published +documents, for as long as that variant stays selected. Selecting a +different variant refreshes the list to that variant’s own documents, +without a page reload.
  4. +
+
+
+

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

+
    +
  • Camptocamp
  • +
+
+ +
+

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_product_document_variant/static/src/interactions/website_sale.esm.js b/website_sale_product_document_variant/static/src/interactions/website_sale.esm.js new file mode 100644 index 0000000000..9ccd3e46cc --- /dev/null +++ b/website_sale_product_document_variant/static/src/interactions/website_sale.esm.js @@ -0,0 +1,40 @@ +import {WebsiteSale} from "@website_sale/interactions/website_sale"; +import {markup} from "@odoo/owl"; +import {patch} from "@web/core/utils/patch"; +import {setElementContent} from "@web/core/utils/html"; + +patch(WebsiteSale.prototype, { + /** + * Adds the documents section refresh to the regular + * _onChangeCombination method + * @override + */ + _onChangeCombination(...args) { + super._onChangeCombination(...args); + this._onChangeCombinationDocuments(...args); + }, + + /** + * Refreshes the "Documents" section to match the newly selected + * variant, hiding it entirely when that variant (combined with the + * template) has nothing published. + * + * @param {MouseEvent} ev + * @param {Element} parent + * @param {Object} combination + */ + _onChangeCombinationDocuments(ev, parent, combination) { + if (combination.product_documents_html === undefined) { + return; + } + const documentsEl = document.querySelector("#product_documents"); + if (!documentsEl) { + return; + } + setElementContent(documentsEl, markup(combination.product_documents_html)); + documentsEl.classList.toggle( + "d-none", + !combination.product_documents_html.trim() + ); + }, +}); diff --git a/website_sale_product_document_variant/tests/__init__.py b/website_sale_product_document_variant/tests/__init__.py new file mode 100644 index 0000000000..67d42669e5 --- /dev/null +++ b/website_sale_product_document_variant/tests/__init__.py @@ -0,0 +1 @@ +from . import test_website_sale_product_document_variant diff --git a/website_sale_product_document_variant/tests/test_website_sale_product_document_variant.py b/website_sale_product_document_variant/tests/test_website_sale_product_document_variant.py new file mode 100644 index 0000000000..75eabf443e --- /dev/null +++ b/website_sale_product_document_variant/tests/test_website_sale_product_document_variant.py @@ -0,0 +1,190 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import base64 +import json +from urllib.parse import urlparse + +from lxml import html + +from odoo.fields import Command +from odoo.tests import HttpCase, tagged + +FAKE_CONTENT = base64.b64encode(b"fake document content") + + +@tagged("post_install", "-at_install") +class TestWebsiteSaleProductDocumentVariantHttp(HttpCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.Document = cls.env["product.document"] + size_attribute = cls.env["product.attribute"].create( + { + "name": "Size", + "value_ids": [ + Command.create({"name": "Small"}), + Command.create({"name": "Large"}), + ], + } + ) + cls.product = cls.env["product.template"].create( + { + "name": "Documented Variant Product", + "type": "consu", + "website_published": True, + "sale_ok": True, + "attribute_line_ids": [ + Command.create( + { + "attribute_id": size_attribute.id, + "value_ids": [Command.set(size_attribute.value_ids.ids)], + } + ), + ], + } + ) + cls.small, cls.large = cls.product.product_variant_ids + cls.Document.create( + { + "name": "General Manual.pdf", + "datas": FAKE_CONTENT, + "res_model": "product.template", + "res_id": cls.product.id, + "shown_on_product_page": True, + } + ) + cls.small_document = cls.Document.create( + { + "name": "Small Size Chart.pdf", + "datas": FAKE_CONTENT, + "res_model": "product.product", + "res_id": cls.small.id, + "shown_on_product_page": True, + } + ) + cls.product_without_documents = cls.env["product.template"].create( + { + "name": "Undocumented Product", + "type": "consu", + "website_published": True, + "sale_ok": True, + } + ) + + @staticmethod + def _make_jsonrpc_payload(params): + return json.dumps({"jsonrpc": "2.0", "method": "call", "params": params}) + + def test_variant_and_template_documents_shown_together(self): + self.authenticate(None, None) + response = self.url_open( + f"{self.product.website_url}" + f"#attribute_values={self.small.product_template_attribute_value_ids.id}" + ) + content = response.text + self.assertIn("General Manual.pdf", content) + self.assertIn("Small Size Chart.pdf", content) + + def test_no_shown_documents_hides_section(self): + """The container is always rendered (see + test_container_present_even_without_shown_documents below) but must + stay visually hidden and empty of document links when the current + combination has nothing published. + + Scoped to the "#product_documents" node itself: this page also has + unrelated "list-group" elements elsewhere (e.g. website menus), so a + page-wide substring check would give a false negative. + """ + self.authenticate(None, None) + response = self.url_open(self.product_without_documents.website_url) + documents_el = html.fromstring(response.content).get_element_by_id( + "product_documents" + ) + self.assertIn("d-none", documents_el.get("class", "")) + self.assertFalse(documents_el.find_class("list-group")) + + def test_container_present_even_without_shown_documents(self): + """The AJAX refresh JS looks up "#product_documents" by id unconditionally. + If the container is absent from the initial render whenever + the starting combination has no documents, switching + to a variant that does have a published document has no target node + to inject it into, and the document silently never appears.""" + self.authenticate(None, None) + response = self.url_open(self.product_without_documents.website_url) + self.assertIn('id="product_documents"', response.text) + + def test_get_variant_documents_html_recomputes_by_variant(self): + """`_get_variant_documents_html` must return content matching the + given variant, so the front-end can refresh the documents section + when the customer changes variant.""" + html_small = self.product._get_variant_documents_html(self.small) + html_large = self.product._get_variant_documents_html(self.large) + self.assertIn("General Manual.pdf", html_small) + self.assertIn("Small Size Chart.pdf", html_small) + self.assertIn("General Manual.pdf", html_large) + self.assertNotIn("Small Size Chart.pdf", html_large) + + def test_get_variant_documents_html_empty_for_no_documents(self): + html = self.product_without_documents._get_variant_documents_html( + self.env["product.product"] + ) + self.assertNotIn("list-group", html) + + def test_get_combination_info_carries_documents_html(self): + self.authenticate(None, None) + small_ptav = self.small.product_template_attribute_value_ids + large_ptav = self.large.product_template_attribute_value_ids + response_small = self.url_open( + "/website_sale/get_combination_info", + data=self._make_jsonrpc_payload( + { + "product_template_id": self.product.id, + "product_id": self.small.id, + "combination": small_ptav.ids, + "add_qty": 1, + } + ), + headers={"Content-Type": "application/json"}, + ) + response_large = self.url_open( + "/website_sale/get_combination_info", + data=self._make_jsonrpc_payload( + { + "product_template_id": self.product.id, + "product_id": self.large.id, + "combination": large_ptav.ids, + "add_qty": 1, + } + ), + headers={"Content-Type": "application/json"}, + ) + html_small = response_small.json()["result"]["product_documents_html"] + html_large = response_large.json()["result"]["product_documents_html"] + self.assertIn("Small Size Chart.pdf", html_small) + self.assertNotIn("Small Size Chart.pdf", html_large) + + def test_published_variant_document_downloads(self): + self.authenticate(None, None) + response = self.url_open( + f"/shop/{self.product.id}/document/{self.small_document.id}" + ) + + self.assertEqual(response.status_code, 200) + # The download route itself lives under "/shop/...", so a plain + # substring check against "/shop" would always match. Check the + # response wasn't redirected to the bare shop root instead. + self.assertNotEqual(urlparse(response.url).path, "/shop") + + def test_unpublished_variant_document_redirects_to_shop(self): + unpublished = self.Document.create( + { + "name": "Draft Notes.pdf", + "datas": FAKE_CONTENT, + "res_model": "product.product", + "res_id": self.small.id, + "shown_on_product_page": False, + } + ) + self.authenticate(None, None) + response = self.url_open(f"/shop/{self.product.id}/document/{unpublished.id}") + self.assertIn("/shop", response.url) diff --git a/website_sale_product_document_variant/views/product_document_views.xml b/website_sale_product_document_variant/views/product_document_views.xml new file mode 100644 index 0000000000..1db8f35fa9 --- /dev/null +++ b/website_sale_product_document_variant/views/product_document_views.xml @@ -0,0 +1,35 @@ + + + + product.document + + + + 0 + + + + + + product.document + + + + 0 + + + + + + product.document + + + + 0 + + + + diff --git a/website_sale_product_document_variant/views/templates.xml b/website_sale_product_document_variant/views/templates.xml new file mode 100644 index 0000000000..8a721b8b4f --- /dev/null +++ b/website_sale_product_document_variant/views/templates.xml @@ -0,0 +1,74 @@ + + + + + + +