From 9491feb95a8d647999592d2cbc7f8df6cf357849 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 23 Jan 2025 17:17:13 +0100 Subject: [PATCH] [ADD] website_sale_product_matrix --- .../odoo/addons/website_sale_product_matrix | 1 + setup/website_sale_product_matrix/setup.py | 6 + website_sale_product_matrix/README.rst | 104 ++++ website_sale_product_matrix/__init__.py | 1 + website_sale_product_matrix/__manifest__.py | 23 + .../controllers/__init__.py | 1 + .../controllers/main.py | 30 ++ .../readme/CONFIGURE.md | 5 + .../readme/CONTRIBUTORS.md | 2 + .../readme/DESCRIPTION.md | 2 + website_sale_product_matrix/readme/USAGE.md | 5 + .../static/description/index.html | 448 ++++++++++++++++++ .../static/src/js/website_sale.esm.js | 59 +++ .../templates/product_template.xml | 141 ++++++ 14 files changed, 828 insertions(+) create mode 120000 setup/website_sale_product_matrix/odoo/addons/website_sale_product_matrix create mode 100644 setup/website_sale_product_matrix/setup.py create mode 100644 website_sale_product_matrix/README.rst create mode 100644 website_sale_product_matrix/__init__.py create mode 100644 website_sale_product_matrix/__manifest__.py create mode 100644 website_sale_product_matrix/controllers/__init__.py create mode 100644 website_sale_product_matrix/controllers/main.py create mode 100644 website_sale_product_matrix/readme/CONFIGURE.md create mode 100644 website_sale_product_matrix/readme/CONTRIBUTORS.md create mode 100644 website_sale_product_matrix/readme/DESCRIPTION.md create mode 100644 website_sale_product_matrix/readme/USAGE.md create mode 100644 website_sale_product_matrix/static/description/index.html create mode 100644 website_sale_product_matrix/static/src/js/website_sale.esm.js create mode 100644 website_sale_product_matrix/templates/product_template.xml diff --git a/setup/website_sale_product_matrix/odoo/addons/website_sale_product_matrix b/setup/website_sale_product_matrix/odoo/addons/website_sale_product_matrix new file mode 120000 index 0000000000..8d94a91199 --- /dev/null +++ b/setup/website_sale_product_matrix/odoo/addons/website_sale_product_matrix @@ -0,0 +1 @@ +../../../../website_sale_product_matrix \ No newline at end of file diff --git a/setup/website_sale_product_matrix/setup.py b/setup/website_sale_product_matrix/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/website_sale_product_matrix/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/website_sale_product_matrix/README.rst b/website_sale_product_matrix/README.rst new file mode 100644 index 0000000000..649d5b668a --- /dev/null +++ b/website_sale_product_matrix/README.rst @@ -0,0 +1,104 @@ +=========================== +Product matrix in eCommerce +=========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:8f1baf6f3c247066848c48779a1a1441552dbb72dceefd3c7143c851d0fb04d5 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fe--commerce-lightgray.png?logo=github + :target: https://github.com/OCA/e-commerce/tree/15.0/website_sale_product_matrix + :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-15-0/e-commerce-15-0-website_sale_product_matrix + :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=15.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +With this module we'll be able to use the product matrix in the +eCommerce so our customers can easily add products to their carts. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure this module, you need to: + +- Go to a product with variants or create one. +- In the *Attributes and variants* tab, *Sales Variant Selection* + section, select **Order grid entry**. + +Usage +===== + +To use this module, you need to: + +1. Go to the configured product web page. +2. You'll se the matrix where you can add units to the cart. + +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 +------- + +* Tecnativa + +Contributors +------------ + +- Tecnativa (https://www.tecnativa.com) + + - David Vidal + +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-chienandalu| image:: https://github.com/chienandalu.png?size=40px + :target: https://github.com/chienandalu + :alt: chienandalu + +Current `maintainer `__: + +|maintainer-chienandalu| + +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_matrix/__init__.py b/website_sale_product_matrix/__init__.py new file mode 100644 index 0000000000..e046e49fbe --- /dev/null +++ b/website_sale_product_matrix/__init__.py @@ -0,0 +1 @@ +from . import controllers diff --git a/website_sale_product_matrix/__manifest__.py b/website_sale_product_matrix/__manifest__.py new file mode 100644 index 0000000000..416b7e186c --- /dev/null +++ b/website_sale_product_matrix/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright 2024 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +{ + "name": "Product matrix in eCommerce", + "version": "15.0.1.0.0", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "website": "https://github.com/OCA/e-commerce", + "category": "Website", + "maintainers": ["chienandalu"], + "depends": [ + "sale_product_matrix", + "website_sale", + ], + "data": [ + "templates/product_template.xml", + ], + "assets": { + "web.assets_frontend": [ + "website_sale_product_matrix/static/src/js/**/*.js", + ], + }, +} diff --git a/website_sale_product_matrix/controllers/__init__.py b/website_sale_product_matrix/controllers/__init__.py new file mode 100644 index 0000000000..12a7e529b6 --- /dev/null +++ b/website_sale_product_matrix/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/website_sale_product_matrix/controllers/main.py b/website_sale_product_matrix/controllers/main.py new file mode 100644 index 0000000000..7579a5c531 --- /dev/null +++ b/website_sale_product_matrix/controllers/main.py @@ -0,0 +1,30 @@ +# Copyright 2025 Tecnativa - David Vidal +# 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 WebsiteSaleProductMatrix(WebsiteSale): + @route( + ["/shop/cart/update_from_matrix"], + type="http", + auth="public", + methods=["POST"], + website=True, + ) + def cart_update_from_matrix(self, product_template_id, grid, **kw): + """This route is called when adding a product to cart from the product matrix""" + sale_order = request.website.sale_get_order(force_create=True) + if sale_order.state != "draft": + request.session["sale_order_id"] = None + sale_order = request.website.sale_get_order(force_create=True) + sale_order.update( + { + "grid_product_tmpl_id": int(product_template_id), + "grid_update": True, + "grid": grid, + } + ) + sale_order._apply_grid() + return request.redirect("/shop/cart") diff --git a/website_sale_product_matrix/readme/CONFIGURE.md b/website_sale_product_matrix/readme/CONFIGURE.md new file mode 100644 index 0000000000..220cdba07e --- /dev/null +++ b/website_sale_product_matrix/readme/CONFIGURE.md @@ -0,0 +1,5 @@ +To configure this module, you need to: + +- Go to a product with variants or create one. +- In the *Attributes and variants* tab, *Sales Variant Selection* section, select + **Order grid entry**. diff --git a/website_sale_product_matrix/readme/CONTRIBUTORS.md b/website_sale_product_matrix/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..9967361ba9 --- /dev/null +++ b/website_sale_product_matrix/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +* Tecnativa (https://www.tecnativa.com) + * David Vidal diff --git a/website_sale_product_matrix/readme/DESCRIPTION.md b/website_sale_product_matrix/readme/DESCRIPTION.md new file mode 100644 index 0000000000..37e4438bd6 --- /dev/null +++ b/website_sale_product_matrix/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +With this module we'll be able to use the product matrix in the eCommerce so our customers +can easily add products to their carts. diff --git a/website_sale_product_matrix/readme/USAGE.md b/website_sale_product_matrix/readme/USAGE.md new file mode 100644 index 0000000000..aa24f5f64e --- /dev/null +++ b/website_sale_product_matrix/readme/USAGE.md @@ -0,0 +1,5 @@ +To use this module, you need to: + +1. Go to the configured product web page. +2. You'll se the matrix where you can add units to the cart. + diff --git a/website_sale_product_matrix/static/description/index.html b/website_sale_product_matrix/static/description/index.html new file mode 100644 index 0000000000..c3c8f5773e --- /dev/null +++ b/website_sale_product_matrix/static/description/index.html @@ -0,0 +1,448 @@ + + + + + +Product matrix in eCommerce + + + +
+

Product matrix in eCommerce

+ + +

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

+

With this module we’ll be able to use the product matrix in the +eCommerce so our customers can easily add products to their carts.

+

Table of contents

+ +
+

Configuration

+

To configure this module, you need to:

+
    +
  • Go to a product with variants or create one.
  • +
  • In the Attributes and variants tab, Sales Variant Selection +section, select Order grid entry.
  • +
+
+
+

Usage

+

To use this module, you need to:

+
    +
  1. Go to the configured product web page.
  2. +
  3. You’ll se the matrix where you can add units to the cart.
  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

+
    +
  • Tecnativa
  • +
+
+
+

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:

+

chienandalu

+

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_matrix/static/src/js/website_sale.esm.js b/website_sale_product_matrix/static/src/js/website_sale.esm.js new file mode 100644 index 0000000000..496798880c --- /dev/null +++ b/website_sale_product_matrix/static/src/js/website_sale.esm.js @@ -0,0 +1,59 @@ +/** @odoo-module **/ +import publicWidget from "web.public.widget"; +import wUtils from "website.utils"; + +// If next dependency is not declared publicWidget.registry.WebsiteSale will be +// undefined +import "website_sale.website_sale"; + +// TODO: Add logic to improve UX +publicWidget.registry.WebsiteSale.include({ + events: Object.assign({}, publicWidget.registry.WebsiteSale.prototype.events, { + "click .o_we_order_matrix": "addToCartFromMatrix", + }), + /** + * Avoid stay on page when the matrix is loaded so we refresh the page every time + * @override + */ + async start() { + await this._super.apply(this, arguments); + const html = document.documentElement; + if (Object.hasOwn(html.dataset, "productAddMode")) { + this.stayOnPageOption = + this.stayOnPageOption && html.dataset.productAddMode !== "matrix"; + } + }, + /** + * Overridable method to add stuff to the grid + * @param {Number} product_template_id + * @param {HTMLFormElement} $form + * @returns {Object} + */ + _parseGridChanges(product_template_id, $form) { + const inputs = $form.getElementsByClassName("o_matrix_input"); + const changes = Array.from(inputs).map((input) => { + return { + qty: parseInt(input.value, 10) || 0, + ptav_ids: JSON.parse(input.dataset.ptav_ids), + }; + }); + return { + product_template_id: product_template_id, + changes: changes, + }; + }, + /** + * Parse the grid with the changes to apply into the order + * @param {Event} ev + * @returns {Promise} + */ + addToCartFromMatrix(ev) { + const $form = ev.currentTarget.closest("form"); + const params = { + product_template_id: parseInt($form.dataset.product_template_id, 10), + }; + const grid = this._parseGridChanges(params.product_template_id, $form); + params.grid = JSON.stringify(grid); + return wUtils.sendRequest("/shop/cart/update_from_matrix", params); + }, +}); diff --git a/website_sale_product_matrix/templates/product_template.xml b/website_sale_product_matrix/templates/product_template.xml new file mode 100644 index 0000000000..c3d2875d17 --- /dev/null +++ b/website_sale_product_matrix/templates/product_template.xml @@ -0,0 +1,141 @@ + + + + + +