From 56b9678287b66f9ad6cd28144a2336dc3be3b685 Mon Sep 17 00:00:00 2001 From: AlexGarS73 Date: Tue, 7 Jul 2026 08:42:10 +0200 Subject: [PATCH] [IMP] website_sale_product_minimal_price: Add to searchbar --- .../models/product_template.py | 27 +++++++ .../tests/test_product_template.py | 76 +++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/website_sale_product_minimal_price/models/product_template.py b/website_sale_product_minimal_price/models/product_template.py index fa61ca6031..385d6a4d8d 100644 --- a/website_sale_product_minimal_price/models/product_template.py +++ b/website_sale_product_minimal_price/models/product_template.py @@ -187,6 +187,33 @@ def _get_combination_info( ) return combination_info + def _get_minimal_search_price(self, website, mapping): + self.ensure_one() + pricelist = self._get_website_current_pricelist(website) + product, add_qty, _has_distinct_price = self._get_cheapest_info(pricelist) + product_price = product.with_context( + quantity=add_qty, pricelist=pricelist.id + )._get_contextual_price() + return self.env["ir.qweb.field.monetary"].value_to_html( + product_price, + {"display_currency": mapping["detail"]["display_currency"]}, + ) + + def _search_render_results(self, fetch_fields, mapping, icon, limit): + results_data = super()._search_render_results( + fetch_fields, mapping, icon, limit + ) + if "detail" not in mapping: + return results_data + + current_website = self.env["website"].get_current_website() + for product, data in zip(self, results_data, strict=False): + minimal_price = product._get_minimal_search_price(current_website, mapping) + if minimal_price: + data["price"] = minimal_price + data.pop("list_price", None) + return results_data + def _get_sales_prices(self, website): prices = super()._get_sales_prices(website) pricelist = self._get_website_current_pricelist(website) diff --git a/website_sale_product_minimal_price/tests/test_product_template.py b/website_sale_product_minimal_price/tests/test_product_template.py index 09b14bcf7e..4572a8c1eb 100644 --- a/website_sale_product_minimal_price/tests/test_product_template.py +++ b/website_sale_product_minimal_price/tests/test_product_template.py @@ -179,3 +179,79 @@ def test_combination_info_lines(self): )._get_combination_info(product_id=self.variant1.id) self.assertIn("minimal_price_scale", res5) self.assertTrue(len(res5["minimal_price_scale"]) > 0) + + def test_search_render_results_uses_minimal_variant_price(self): + from odoo.addons.website_sale.tests.common import MockRequest + + pl = self.env["product.pricelist"].create( + { + "name": "Search Minimal PL", + "item_ids": [ + ( + 0, + 0, + { + "applied_on": "0_product_variant", + "product_id": self.variant1.id, + "compute_price": "fixed", + "fixed_price": 50.0, + }, + ) + ], + } + ) + mapping = { + "name": {"name": "name", "type": "text"}, + "website_url": {"name": "website_url", "type": "text"}, + "detail": { + "name": "price", + "type": "html", + "display_currency": self.website.currency_id, + }, + } + with patch.object( + type(self.product_tmpl), "_get_website_current_pricelist" + ) as mock_pricelist: + mock_pricelist.return_value = pl + with MockRequest(self.env, website=self.website): + result = self.product_tmpl._search_render_results( + ["id", "name", "website_url"], mapping, "fa-shopping-cart", 5 + )[0] + + self.assertNotIn("From", result["price"]) + self.assertIn("50.00", result["price"]) + + def test_minimal_search_price_without_distinct_prices(self): + pl = self.env["product.pricelist"].create({"name": "Search Regular PL"}) + mapping = { + "detail": { + "display_currency": self.website.currency_id, + }, + } + with patch.object( + type(self.product_tmpl), "_get_website_current_pricelist" + ) as mock_pricelist: + mock_pricelist.return_value = pl + with patch.object( + type(self.product_tmpl), "_get_cheapest_info" + ) as mock_cheap: + mock_cheap.return_value = (self.variant1, 1, False) + result = self.product_tmpl._get_minimal_search_price( + self.website, mapping + ) + + self.assertIn("100.00", result) + + def test_search_render_results_without_price_detail(self): + from odoo.addons.website_sale.tests.common import MockRequest + + mapping = { + "name": {"name": "name", "type": "text"}, + "website_url": {"name": "website_url", "type": "text"}, + } + with MockRequest(self.env, website=self.website): + result = self.product_tmpl._search_render_results( + ["id", "name", "website_url"], mapping, "fa-shopping-cart", 5 + )[0] + + self.assertNotIn("price", result)