Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions website_sale_product_minimal_price/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
76 changes: 76 additions & 0 deletions website_sale_product_minimal_price/tests/test_product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading