From d3d21b6d3ad22d1b4a47aa6a59910a645765c2b9 Mon Sep 17 00:00:00 2001 From: edward_yoch Date: Mon, 24 Jun 2024 11:45:29 +0000 Subject: [PATCH] [MIG] website_sale_product_minimal_price: Migration to 17.0 --- .../__manifest__.py | 8 +- .../controllers/main.py | 82 +++---------- website_sale_product_minimal_price/i18n/ca.po | 12 +- website_sale_product_minimal_price/i18n/es.po | 12 +- website_sale_product_minimal_price/i18n/fr.po | 12 +- website_sale_product_minimal_price/i18n/it.po | 13 +- website_sale_product_minimal_price/i18n/nl.po | 14 +-- website_sale_product_minimal_price/i18n/pt.po | 25 ++-- .../website_sale_product_minimal_price.pot | 11 +- .../models/__init__.py | 1 + .../models/product_product.py | 56 +++++++++ .../models/product_template.py | 56 +++++++-- .../tours/test_product_with_no_prices_tour.js | 25 ++-- .../static/src/js/tours/tour.js | 26 ++-- .../js/website_sale_product_minimal_price.js | 81 ------------- .../js/website_sale_product_price_scale.js | 112 +++++++----------- .../website_sale_product_minimal_price.xml | 9 -- .../xml/website_sale_product_price_scale.xml | 44 +++++-- .../tests/test_product_with_no_prices.py | 2 +- ...test_website_sale_product_minimal_price.py | 4 +- .../views/templates.xml | 45 +++++-- 21 files changed, 293 insertions(+), 357 deletions(-) create mode 100644 website_sale_product_minimal_price/models/product_product.py delete mode 100644 website_sale_product_minimal_price/static/src/js/website_sale_product_minimal_price.js delete mode 100644 website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml diff --git a/website_sale_product_minimal_price/__manifest__.py b/website_sale_product_minimal_price/__manifest__.py index 009c04a122..59de2ccbb2 100644 --- a/website_sale_product_minimal_price/__manifest__.py +++ b/website_sale_product_minimal_price/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Website Sale Product Minimal Price", "summary": "Display minimal price for products that has variants", - "version": "16.0.1.0.0", + "version": "17.0.1.0.0", "development_status": "Production/Stable", "maintainers": ["sergio-teruel"], "category": "Website", @@ -16,9 +16,9 @@ "data": ["views/templates.xml"], "assets": { "web.assets_frontend": [ - "/web/static/src/legacy/js/fields/field_utils.js", - "/website_sale_product_minimal_price/static/src/xml/*.xml", - "/website_sale_product_minimal_price/static/src/js/*.js", + "/web/static/src/views/fields/formatters.js", + "/website_sale_product_minimal_price/static/src/xml/website_sale_product_price_scale.xml", + "/website_sale_product_minimal_price/static/src/js/website_sale_product_price_scale.js", ], "web.assets_tests": [ "/website_sale_product_minimal_price/static/src/js/tours/*.js" diff --git a/website_sale_product_minimal_price/controllers/main.py b/website_sale_product_minimal_price/controllers/main.py index 081227b8b6..497db50f22 100644 --- a/website_sale_product_minimal_price/controllers/main.py +++ b/website_sale_product_minimal_price/controllers/main.py @@ -4,46 +4,12 @@ from odoo import http from odoo.http import request -from odoo.addons.sale.controllers.variant import VariantController +from odoo.addons.website_sale.controllers.variant import WebsiteSaleVariantController -class WebsiteSaleVariantController(VariantController): +class WebsiteSaleVariantController(WebsiteSaleVariantController): @http.route( - ["/sale/get_combination_info_minimal_price"], - type="json", - auth="public", - methods=["POST"], - website=True, - ) - def get_combination_info_minimal_price(self, product_template_ids, **kw): - """Special route to use website logic in get_combination_info override. - This route is called in JS by appending _website to the base route. - """ - res = [] - templates = request.env["product.template"].sudo().browse(product_template_ids) - pricelist = request.env["website"].get_current_website().get_current_pricelist() - for template in templates.filtered(lambda t: t.is_published): - product_id, add_qty, has_distinct_price = template._get_cheapest_info( - pricelist - ) - combination = template._get_combination_info( - product_id=product_id, add_qty=add_qty, pricelist=pricelist - ) - res.append( - { - "id": template.id, - "price": combination.get("price"), - "distinct_prices": has_distinct_price, - "currency": { - "position": template.currency_id.position, - "symbol": template.currency_id.symbol, - }, - } - ) - return res - - @http.route( - ["/sale/get_combination_info_pricelist_atributes"], + ["/website_sale/get_combination_info_pricelist_atributes"], type="json", auth="public", website=True, @@ -52,7 +18,15 @@ def get_combination_info_pricelist_atributes(self, product_id, **kwargs): """Special route to use website logic in get_combination_info override. This route is called in JS by appending _website to the base route. """ - pricelist = request.env["website"].get_current_website().get_current_pricelist() + # Copied from _get_combination_info + # /odoo/addons/website_sale/models/product_template.py + website = ( + # pylint: disable=W8121 + request.env["website"] + .get_current_website() + .with_context(request.env.context) + ) + pricelist = website.pricelist_id product = ( request.env["product.product"] .browse(product_id) @@ -61,34 +35,8 @@ def get_combination_info_pricelist_atributes(self, product_id, **kwargs): # Getting all min_quantity of the current product to compute the possible # price scale. qty_list = request.env["product.pricelist.item"].search( - [ - "|", - ("product_id", "=", product.id), - "|", - ("product_tmpl_id", "=", product.product_tmpl_id.id), - ( - "categ_id", - "in", - list(map(int, product.categ_id.parent_path.split("/")[0:-1])), - ), - ("min_quantity", ">", 0), - ] + product._get_product_pricelist_item_domain() ) qty_list = sorted(set(qty_list.mapped("min_quantity"))) - res = [] - last_price = product.with_context(quantity=0)._get_contextual_price() - for min_qty in qty_list: - new_price = product.with_context(quantity=min_qty)._get_contextual_price() - if new_price != last_price: - res.append( - { - "min_qty": min_qty, - "price": new_price, - "currency": { - "position": product.currency_id.position, - "symbol": product.currency_id.symbol, - }, - } - ) - last_price = new_price - return (res, product.uom_name) + + return product._get_product_price_scale(qty_list) diff --git a/website_sale_product_minimal_price/i18n/ca.po b/website_sale_product_minimal_price/i18n/ca.po index ec7a0aabad..8562db800b 100644 --- a/website_sale_product_minimal_price/i18n/ca.po +++ b/website_sale_product_minimal_price/i18n/ca.po @@ -4,7 +4,7 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 13.0\n" +"Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2021-05-17 14:53+0000\n" "PO-Revision-Date: 2021-05-17 16:54+0200\n" @@ -18,9 +18,7 @@ msgstr "" "X-Generator: Poedit 2.0.6\n" #. module: website_sale_product_minimal_price -#. odoo-javascript -#: code:addons/website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml:0 -#, python-format +#: model_terms:ir.ui.view,arch_db:website_sale_product_minimal_price.products_item msgid "From" msgstr "De" @@ -36,5 +34,7 @@ msgstr "Preus per quantitat (" msgid "Product" msgstr "" -#~ msgid "Product Template" -#~ msgstr "Plantilla de producte" +#. module: website_sale_product_minimal_price +#: model:ir.model,name:website_sale_product_minimal_price.model_product_product +msgid "Product Variant" +msgstr "" diff --git a/website_sale_product_minimal_price/i18n/es.po b/website_sale_product_minimal_price/i18n/es.po index 3c5c320866..6679fd5ab3 100644 --- a/website_sale_product_minimal_price/i18n/es.po +++ b/website_sale_product_minimal_price/i18n/es.po @@ -4,7 +4,7 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 13.0+e\n" +"Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2021-05-17 14:53+0000\n" "PO-Revision-Date: 2021-05-17 16:54+0200\n" @@ -18,9 +18,7 @@ msgstr "" "X-Generator: Poedit 2.0.6\n" #. module: website_sale_product_minimal_price -#. odoo-javascript -#: code:addons/website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml:0 -#, python-format +#: model_terms:ir.ui.view,arch_db:website_sale_product_minimal_price.products_item msgid "From" msgstr "Desde" @@ -36,5 +34,7 @@ msgstr "Precios por cantidad (" msgid "Product" msgstr "" -#~ msgid "Product Template" -#~ msgstr "Plantilla de producto" +#. module: website_sale_product_minimal_price +#: model:ir.model,name:website_sale_product_minimal_price.model_product_product +msgid "Product Variant" +msgstr "" diff --git a/website_sale_product_minimal_price/i18n/fr.po b/website_sale_product_minimal_price/i18n/fr.po index 816fbaf98a..2ffb5f7ee1 100644 --- a/website_sale_product_minimal_price/i18n/fr.po +++ b/website_sale_product_minimal_price/i18n/fr.po @@ -4,7 +4,7 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 15.0\n" +"Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" "PO-Revision-Date: 2023-09-03 13:35+0000\n" "Last-Translator: benj-filament \n" @@ -17,9 +17,7 @@ msgstr "" "X-Generator: Weblate 4.17\n" #. module: website_sale_product_minimal_price -#. odoo-javascript -#: code:addons/website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml:0 -#, python-format +#: model_terms:ir.ui.view,arch_db:website_sale_product_minimal_price.products_item msgid "From" msgstr "À partir de" @@ -35,5 +33,7 @@ msgstr "Prix par quantité (" msgid "Product" msgstr "" -#~ msgid "Product Template" -#~ msgstr "Modèle d'article" +#. module: website_sale_product_minimal_price +#: model:ir.model,name:website_sale_product_minimal_price.model_product_product +msgid "Product Variant" +msgstr "" diff --git a/website_sale_product_minimal_price/i18n/it.po b/website_sale_product_minimal_price/i18n/it.po index 7fef835fef..b7ea7bcfc0 100644 --- a/website_sale_product_minimal_price/i18n/it.po +++ b/website_sale_product_minimal_price/i18n/it.po @@ -4,7 +4,7 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 16.0\n" +"Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" "PO-Revision-Date: 2024-05-08 16:34+0000\n" "Last-Translator: mymage \n" @@ -17,9 +17,7 @@ msgstr "" "X-Generator: Weblate 4.17\n" #. module: website_sale_product_minimal_price -#. odoo-javascript -#: code:addons/website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml:0 -#, python-format +#: model_terms:ir.ui.view,arch_db:website_sale_product_minimal_price.products_item msgid "From" msgstr "Dal" @@ -33,4 +31,9 @@ msgstr "Prezzi per quantità (" #. module: website_sale_product_minimal_price #: model:ir.model,name:website_sale_product_minimal_price.model_product_template msgid "Product" -msgstr "Prodotto" +msgstr "" + +#. module: website_sale_product_minimal_price +#: model:ir.model,name:website_sale_product_minimal_price.model_product_product +msgid "Product Variant" +msgstr "" diff --git a/website_sale_product_minimal_price/i18n/nl.po b/website_sale_product_minimal_price/i18n/nl.po index 5995633326..95e123adb9 100644 --- a/website_sale_product_minimal_price/i18n/nl.po +++ b/website_sale_product_minimal_price/i18n/nl.po @@ -4,7 +4,7 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 13.0\n" +"Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2021-05-17 14:53+0000\n" "PO-Revision-Date: 2021-05-17 16:55+0200\n" @@ -18,9 +18,7 @@ msgstr "" "X-Generator: Poedit 2.0.6\n" #. module: website_sale_product_minimal_price -#. odoo-javascript -#: code:addons/website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml:0 -#, python-format +#: model_terms:ir.ui.view,arch_db:website_sale_product_minimal_price.products_item msgid "From" msgstr "Van" @@ -29,12 +27,14 @@ msgstr "Van" #: code:addons/website_sale_product_minimal_price/static/src/xml/website_sale_product_price_scale.xml:0 #, python-format msgid "Prices per quantity (" -msgstr "" +msgstr "Prijzen per hoeveelheid (" #. module: website_sale_product_minimal_price #: model:ir.model,name:website_sale_product_minimal_price.model_product_template msgid "Product" msgstr "" -#~ msgid "Product Template" -#~ msgstr "Product Sjabloon" +#. module: website_sale_product_minimal_price +#: model:ir.model,name:website_sale_product_minimal_price.model_product_product +msgid "Product Variant" +msgstr "" diff --git a/website_sale_product_minimal_price/i18n/pt.po b/website_sale_product_minimal_price/i18n/pt.po index d013857b84..f246b21f15 100644 --- a/website_sale_product_minimal_price/i18n/pt.po +++ b/website_sale_product_minimal_price/i18n/pt.po @@ -4,7 +4,7 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 14.0\n" +"Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" "PO-Revision-Date: 2022-08-22 14:07+0000\n" "Last-Translator: Pedro Castro Silva \n" @@ -17,11 +17,9 @@ msgstr "" "X-Generator: Weblate 4.3.2\n" #. module: website_sale_product_minimal_price -#. odoo-javascript -#: code:addons/website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml:0 -#, python-format +#: model_terms:ir.ui.view,arch_db:website_sale_product_minimal_price.products_item msgid "From" -msgstr "A partir de" +msgstr "A partir d" #. module: website_sale_product_minimal_price #. odoo-javascript @@ -30,19 +28,12 @@ msgstr "A partir de" msgid "Prices per quantity (" msgstr "Preços por quantidade (" +#. module: website_sale_product_minimal_price +#: model:ir.model,name:website_sale_product_minimal_price.model_product_product +msgid "Product Variant" +msgstr "" + #. module: website_sale_product_minimal_price #: model:ir.model,name:website_sale_product_minimal_price.model_product_template msgid "Product" msgstr "" - -#~ msgid "Product Template" -#~ msgstr "Modelo de Produto" - -#~ msgid "Display Name" -#~ msgstr "Nome a Exibir" - -#~ msgid "ID" -#~ msgstr "ID" - -#~ msgid "Last Modified on" -#~ msgstr "Modific. pela última vez em" diff --git a/website_sale_product_minimal_price/i18n/website_sale_product_minimal_price.pot b/website_sale_product_minimal_price/i18n/website_sale_product_minimal_price.pot index 122cb405ad..c145646bb6 100644 --- a/website_sale_product_minimal_price/i18n/website_sale_product_minimal_price.pot +++ b/website_sale_product_minimal_price/i18n/website_sale_product_minimal_price.pot @@ -4,7 +4,7 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 16.0\n" +"Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" "Last-Translator: \n" "Language-Team: \n" @@ -14,9 +14,7 @@ msgstr "" "Plural-Forms: \n" #. module: website_sale_product_minimal_price -#. odoo-javascript -#: code:addons/website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml:0 -#, python-format +#: model_terms:ir.ui.view,arch_db:website_sale_product_minimal_price.products_item msgid "From" msgstr "" @@ -31,3 +29,8 @@ msgstr "" #: model:ir.model,name:website_sale_product_minimal_price.model_product_template msgid "Product" msgstr "" + +#. module: website_sale_product_minimal_price +#: model:ir.model,name:website_sale_product_minimal_price.model_product_product +msgid "Product Variant" +msgstr "" diff --git a/website_sale_product_minimal_price/models/__init__.py b/website_sale_product_minimal_price/models/__init__.py index e8fa8f6bf1..18b37e8532 100644 --- a/website_sale_product_minimal_price/models/__init__.py +++ b/website_sale_product_minimal_price/models/__init__.py @@ -1 +1,2 @@ +from . import product_product from . import product_template diff --git a/website_sale_product_minimal_price/models/product_product.py b/website_sale_product_minimal_price/models/product_product.py new file mode 100644 index 0000000000..2bd9e6b36d --- /dev/null +++ b/website_sale_product_minimal_price/models/product_product.py @@ -0,0 +1,56 @@ +# Copyright 2019 Tecnativa - Sergio Teruel +# Copyright 2020 Tecnativa - Pedro M. Baeza +# Copyright 2021 Tecnativa - Carlos Roca +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import models + +# Added for having easier customizations +COLUMN_LIMIT = 4 +DEFAULT_PRICELIST_TITLE = "website_sale_product_minimal_price.pricelist_title" +DEFAULT_PRICELIST_SCALE = "website_sale_product_minimal_price.pricelist_scale" + + +class ProductProduct(models.Model): + _inherit = "product.product" + + def _get_product_pricelist_item_domain(self): + self.ensure_one() + return [ + "|", + ("product_id", "=", self.id), + "|", + ("product_tmpl_id", "=", self.product_tmpl_id.id), + ( + "categ_id", + "in", + list(map(int, self.categ_id.parent_path.split("/")[0:-1])), + ), + ("min_quantity", ">", 0), + ] + + def _get_product_price_scale(self, qty_list): + self.ensure_one() + unit_prices = [] + last_price = self.with_context(quantity=0)._get_contextual_price() + is_int = self.uom_id == self.env.ref("uom.product_uom_unit") + for min_qty in qty_list: + new_price = self.with_context(quantity=min_qty)._get_contextual_price() + qty_min = int(min_qty) if is_int else min_qty + if new_price != last_price and new_price != 0: + unit_prices.append( + { + "qty_str": f"≥{qty_min}", + "qty_min": qty_min, # Added for having easier customizations + "price": new_price, + "currency_id": self.currency_id.id, + } + ) + last_price = new_price + + return { + "uom": self.uom_name, + "column_limit": COLUMN_LIMIT, + "pricelist_title": DEFAULT_PRICELIST_TITLE, + "pricelist_scale": DEFAULT_PRICELIST_SCALE, + "unit_prices": unit_prices, + } diff --git a/website_sale_product_minimal_price/models/product_template.py b/website_sale_product_minimal_price/models/product_template.py index 9d0c3e4f56..95232e12bb 100644 --- a/website_sale_product_minimal_price/models/product_template.py +++ b/website_sale_product_minimal_price/models/product_template.py @@ -8,6 +8,10 @@ class ProductTemplate(models.Model): _inherit = "product.template" + def _get_cheapest_product_variant_ids(self): + """Added for having easier customizations""" + return self.product_variant_ids + def _get_product_subpricelists(self, pricelist_id): return pricelist_id.item_ids.filtered( lambda i: ( @@ -18,7 +22,7 @@ def _get_product_subpricelists(self, pricelist_id): or (i.applied_on == "1_product" and i.product_tmpl_id == self) or ( i.applied_on == "0_product_variant" - and i.product_id in self.product_variant_ids + and i.product_id in self._get_cheapest_product_variant_ids() ) ) and i.compute_price == "formula" @@ -27,7 +31,7 @@ def _get_product_subpricelists(self, pricelist_id): def _get_variants_from_pricelist(self, pricelist_ids): return pricelist_ids.mapped("item_ids").filtered( - lambda i: i.product_id in self.product_variant_ids + lambda i: i.product_id in self._get_cheapest_product_variant_ids() ) def _get_pricelist_variant_items(self, pricelist_id): @@ -55,8 +59,9 @@ def _get_cheapest_info(self, pricelist): add_qty = 0 has_distinct_price = False # Variants with extra price - variants_extra_price = self.product_variant_ids.filtered("price_extra") - variants_without_extra_price = self.product_variant_ids - variants_extra_price + product_variant_ids = self._get_cheapest_product_variant_ids() + variants_extra_price = product_variant_ids.filtered("price_extra") + variants_without_extra_price = product_variant_ids - variants_extra_price # Avoid compute prices when pricelist has not item variants defined variant_items = self._get_pricelist_variant_items(pricelist) if variant_items: @@ -65,7 +70,7 @@ def _get_cheapest_info(self, pricelist): # category level. Maybe there is any definition on template that # has cheaper price. variants = variant_items.mapped("product_id") - products = variants + (self.product_variant_ids - variants)[:1] + products = variants + (product_variant_ids - variants)[:1] else: products = variants_without_extra_price[:1] products |= variants_extra_price @@ -91,24 +96,49 @@ def _get_first_possible_combination( res = super()._get_first_possible_combination( parent_combination=parent_combination, necessary_values=necessary_values ) - context = self.env.context - if ( - context.get("website_id") - and context.get("pricelist") - and self.product_variant_count > 1 - ): + # Copied from _get_combination_info + # /odoo/addons/website_sale/models/product_template.py + website = ( + # pylint: disable=W8121 + self.env["website"].get_current_website().with_context(self.env.context) + ) + pricelist = website.pricelist_id + if website and pricelist and self.product_variant_count > 1: # It only makes sense to change the default one when there are # more than one variants and we know the pricelist - pricelist = self.env["product.pricelist"].browse(context["pricelist"]) product_id = self._get_cheapest_info(pricelist)[0] product = self.env["product.product"].browse(product_id) # Rebuild the combination in the expected order res = self.env["product.template.attribute.value"] for line in product.valid_product_template_attribute_line_ids: value = product.product_template_attribute_value_ids.filtered( - lambda x: x in line.product_template_value_ids + lambda x, line=line: x in line.product_template_value_ids ) if not value: value = line.product_template_value_ids[:1] res += value return res + + def _get_sales_prices(self, pricelist, fiscal_position): + res = super()._get_sales_prices(pricelist, fiscal_position) + # Copied from _get_combination_info + # /odoo/addons/website_sale/models/product_template.py + website = ( + # pylint: disable=W8121 + self.env["website"].get_current_website().with_context(self.env.context) + ) + if website: + for template in self.filtered( + lambda t: t.is_published and t.id in res and "price_reduce" in res[t.id] + ): + product_id, add_qty, has_distinct_price = template._get_cheapest_info( + pricelist + ) + if has_distinct_price: + distinct_price = template._get_combination_info( + product_id=product_id, + add_qty=add_qty, + ).get("price") + if distinct_price != res[template.id]["price_reduce"]: + res[template.id]["distinct_price"] = distinct_price + return res diff --git a/website_sale_product_minimal_price/static/src/js/tours/test_product_with_no_prices_tour.js b/website_sale_product_minimal_price/static/src/js/tours/test_product_with_no_prices_tour.js index 8cd00875b3..a944797125 100644 --- a/website_sale_product_minimal_price/static/src/js/tours/test_product_with_no_prices_tour.js +++ b/website_sale_product_minimal_price/static/src/js/tours/test_product_with_no_prices_tour.js @@ -1,12 +1,14 @@ +/** @odoo-module **/ + /* Copyright 2021 Carlos Roca * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ -odoo.define("test_product_with_no_prices.tour", function (require) { - "use strict"; - - var tour = require("web_tour.tour"); +import {registry} from "@web/core/registry"; - var steps = [ +registry.category("web_tour.tours").add("test_product_with_no_prices", { + url: "/shop", + test: true, + steps: () => [ { trigger: "a:contains('My product test with no prices')", extra_trigger: ".product_price:has(span:contains('From'))", @@ -19,16 +21,5 @@ odoo.define("test_product_with_no_prices.tour", function (require) { trigger: "a:contains('My product test')", extra_trigger: ".product_price:has(span:contains('10.00'))", }, - ]; - tour.register( - "test_product_with_no_prices", - { - url: "/shop", - test: true, - }, - steps - ); - return { - steps: steps, - }; + ], }); diff --git a/website_sale_product_minimal_price/static/src/js/tours/tour.js b/website_sale_product_minimal_price/static/src/js/tours/tour.js index 4f2a5c484c..9b4545c679 100644 --- a/website_sale_product_minimal_price/static/src/js/tours/tour.js +++ b/website_sale_product_minimal_price/static/src/js/tours/tour.js @@ -1,12 +1,14 @@ +/** @odoo-module **/ + /* Copyright 2019 Sergio Teruel * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ -odoo.define("website_sale_product_minimal_price.tour", function (require) { - "use strict"; - - var tour = require("web_tour.tour"); +import {registry} from "@web/core/registry"; - var steps = [ +registry.category("web_tour.tours").add("test_website_sale_product_minimal_price", { + url: "/shop", + test: true, + steps: () => [ { trigger: ".o_wsale_product_information:has(span:contains('From')) a:contains('My product test with various prices')", @@ -22,17 +24,5 @@ odoo.define("website_sale_product_minimal_price.tour", function (require) { trigger: "a[href='/shop']", extra_trigger: ".product_price:has(span:contains('125.00'))", }, - ]; - - tour.register( - "website_sale_product_minimal_price", - { - url: "/shop", - test: true, - }, - steps - ); - return { - steps: steps, - }; + ], }); diff --git a/website_sale_product_minimal_price/static/src/js/website_sale_product_minimal_price.js b/website_sale_product_minimal_price/static/src/js/website_sale_product_minimal_price.js deleted file mode 100644 index 24ce8eb49c..0000000000 --- a/website_sale_product_minimal_price/static/src/js/website_sale_product_minimal_price.js +++ /dev/null @@ -1,81 +0,0 @@ -odoo.define("website_sale_product_minimal_price.shop_min_price", function (require) { - "use strict"; - - const publicWidget = require("web.public.widget"); - const core = require("web.core"); - const field_utils = require("web.field_utils"); - - publicWidget.registry.WebsiteSaleProductMinimalPrice = publicWidget.Widget.extend({ - selector: "#products_grid", - - start: function () { - return Promise.all([ - this._super.apply(this, arguments), - this.render_price(), - ]); - }, - render_price: function () { - const $products = $(".o_wsale_product_grid_wrapper"); - const product_dic = {}; - $products.each(function () { - product_dic[this.querySelector("a img").src.split("/")[6]] = this; - }); - const product_ids = Object.keys(product_dic).map(Number); - return this._rpc({ - route: "/sale/get_combination_info_minimal_price/", - params: {product_template_ids: product_ids}, - }).then((products_min_price) => { - for (const product of products_min_price) { - if (!product.distinct_prices) { - continue; - } - $(product_dic[product.id]) - .find(".product_price") - .prepend( - $( - core.qweb.render( - "website_sale_product_minimal_price.from_view" - ) - ).get(0) - ); - const $price = $(product_dic[product.id]).find( - ".product_price span .oe_currency_value" - ); - if ($price.length) { - $price.replaceWith( - $( - core.qweb.render( - "website_sale_product_minimal_price.product_minimal_price", - { - price: this.widgetMonetary(product.price, {}), - } - ) - ).get(0) - ); - } else { - let price = this.widgetMonetary(product.price, { - currency: product.currency, - }); - price = price.replace(" ", " "); - $(product_dic[product.id]) - .find(".product_price") - .append( - $( - core.qweb.render( - "website_sale_product_minimal_price.product_minimal_price", - { - price: price, - } - ) - ).get(0) - ); - } - } - return products_min_price; - }); - }, - widgetMonetary: function (amount, format_options) { - return field_utils.format.monetary(amount, {}, format_options); - }, - }); -}); diff --git a/website_sale_product_minimal_price/static/src/js/website_sale_product_price_scale.js b/website_sale_product_minimal_price/static/src/js/website_sale_product_price_scale.js index 22932ca628..0e39db6e2a 100644 --- a/website_sale_product_minimal_price/static/src/js/website_sale_product_price_scale.js +++ b/website_sale_product_minimal_price/static/src/js/website_sale_product_price_scale.js @@ -1,76 +1,44 @@ -odoo.define("website_sale_product_minimal_price.load", function (require) { - "use strict"; - const ajax = require("web.ajax"); - const core = require("web.core"); - const field_utils = require("web.field_utils"); - const publicWidget = require("web.public.widget"); - const VariantMixin = require("sale.VariantMixin"); - const QWeb = core.qweb; +/** @odoo-module **/ - require("website_sale.website_sale"); +import {formatMonetary} from "@web/views/fields/formatters"; +import {jsonrpc} from "@web/core/network/rpc_service"; +import {renderToFragment} from "@web/core/utils/render"; +import VariantMixin from "@website_sale/js/sale_variant_mixin"; +import publicWidget from "@web/legacy/js/public/public_widget"; - VariantMixin._onChangeQtyWebsiteSale = function (ev, $parent, combination) { - if (!this.isWebsite) { - return; - } - ajax.jsonRpc("/sale/get_combination_info_pricelist_atributes", "call", { +VariantMixin._onChangeQtyWebsiteSale = async function (ev, $parent, combination) { + const $node = document.getElementById("js_product_price_scale"); + if (!this.isWebsite || !$node) { + return; + } + const data = await jsonrpc( + "/website_sale/get_combination_info_pricelist_atributes", + { product_id: combination.product_id, - }).then(function (vals) { - const unit_prices = vals[0]; - const uom_name = vals[1]; - $(".temporal").remove(); - if (unit_prices.length > 0) { - const $form = $('form[action*="/shop/cart/update"]'); - $form.append('
'); - $form.append( - QWeb.render("website_sale_product_minimal_price.title", { - uom: uom_name, - }) - ); - // We define a limit of displayed columns as 4 - const limit_col = 4; - let $div; // eslint-disable-line init-declarations - for (const i in unit_prices) { - if (unit_prices[i].price === 0) { - continue; - } - if (i % limit_col === 0) { - const id = i / limit_col; - $form.append( - '
' - ); - $div = $("#row_" + id); - } - let monetary_u = field_utils.format.monetary( - unit_prices[i].price, - {}, - {currency: unit_prices[i].currency} - ); - monetary_u = monetary_u.replace(" ", " "); - $div.append( - QWeb.render("website_sale_product_minimal_price.pricelist", { - quantity: unit_prices[i].min_qty, - price: monetary_u, - }) - ); - } - $div = $('div[id*="row_"]'); - for (let i = 0; i < $div.length - 1; i++) { - $($div[i]).addClass("border-bottom"); - } - } - }); - }; - publicWidget.registry.WebsiteSale.include({ - /** - * Add _onChangeQtyWebsiteSale to _onChangeCombination method. - * - * @override - */ - _onChangeCombination: function () { - this._super.apply(this, arguments); - VariantMixin._onChangeQtyWebsiteSale.apply(this, arguments); - }, - }); - return VariantMixin; + } + ); + const $price_scale = renderToFragment( + "website_sale_product_minimal_price.pricelist_block", + { + ...data, + ...this._additionalProductScaleContext(), + } + ); + $node.innerHTML = ""; + $node.appendChild($price_scale); +}; + +publicWidget.registry.WebsiteSale.include({ + /** + * Add _onChangeQtyWebsiteSale to _onChangeCombination method. + * + * @override + */ + _onChangeCombination: function () { + this._super.apply(this, arguments); + VariantMixin._onChangeQtyWebsiteSale.apply(this, arguments); + }, + _additionalProductScaleContext: function () { + return {formatMonetary}; + }, }); diff --git a/website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml b/website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml deleted file mode 100644 index 36ab698b1a..0000000000 --- a/website_sale_product_minimal_price/static/src/xml/website_sale_product_minimal_price.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - From - - diff --git a/website_sale_product_minimal_price/static/src/xml/website_sale_product_price_scale.xml b/website_sale_product_minimal_price/static/src/xml/website_sale_product_price_scale.xml index fe85987314..ac4350cbdc 100644 --- a/website_sale_product_minimal_price/static/src/xml/website_sale_product_price_scale.xml +++ b/website_sale_product_minimal_price/static/src/xml/website_sale_product_price_scale.xml @@ -1,17 +1,39 @@ - -
- Prices per quantity ( ) -
+ + + - -
-
- -
-
- + + +
+
+ Prices per quantity ( ) +
+
+
+ +
+
+
+ +
+
+ +
diff --git a/website_sale_product_minimal_price/tests/test_product_with_no_prices.py b/website_sale_product_minimal_price/tests/test_product_with_no_prices.py index 57f27f38bf..6e0d04b719 100644 --- a/website_sale_product_minimal_price/tests/test_product_with_no_prices.py +++ b/website_sale_product_minimal_price/tests/test_product_with_no_prices.py @@ -101,4 +101,4 @@ def setUp(self): def test_ui_website(self): """Test frontend tour.""" - self.start_tour("/", "test_product_with_no_prices", login="admin") + self.start_tour("/shop", "test_product_with_no_prices", login="admin") diff --git a/website_sale_product_minimal_price/tests/test_website_sale_product_minimal_price.py b/website_sale_product_minimal_price/tests/test_website_sale_product_minimal_price.py index 894dd804d5..e87a1441f7 100644 --- a/website_sale_product_minimal_price/tests/test_website_sale_product_minimal_price.py +++ b/website_sale_product_minimal_price/tests/test_website_sale_product_minimal_price.py @@ -84,4 +84,6 @@ def setUp(self): def test_ui_website(self): """Test frontend tour.""" - self.start_tour("/shop", "website_sale_product_minimal_price", login="admin") + self.start_tour( + "/shop", "test_website_sale_product_minimal_price", login="admin" + ) diff --git a/website_sale_product_minimal_price/views/templates.xml b/website_sale_product_minimal_price/views/templates.xml index 45306c4194..ff556052c3 100644 --- a/website_sale_product_minimal_price/views/templates.xml +++ b/website_sale_product_minimal_price/views/templates.xml @@ -2,31 +2,52 @@ -