Skip to content

Commit

Permalink
[IMP][FIX] website_sale_product_assortment: handle multiple assortments
Browse files Browse the repository at this point in the history
This commit changes the way multiple assortments are handled and fixes a
couple of bugs in the process.

The current version of the module is not able to handle multiple
assortments, especially ones that are exclusive (e.g. one assortment
with all consumable products and one with all services) and the module
seemingly breaks, ignoring the selected rule (e.g. no_show)

With these changes, that behaviour is fixed, and additionally
assortments for the same website and partner and merged in a logical OR.
  • Loading branch information
aleuffre committed Sep 6, 2023
1 parent 1daa430 commit b84e956
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 34 deletions.
2 changes: 1 addition & 1 deletion website_sale_product_assortment/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"license": "AGPL-3",
"category": "Website",
"website": "https://github.com/OCA/e-commerce",
"author": "Tecnativa, Odoo Community Association (OCA)",
"author": "Tecnativa, PyTech SRL, Ooops404, Odoo Community Association (OCA)",
"maintainers": ["CarlosRoca13"],
"installable": True,
"depends": ["product_assortment", "website_sale"],
Expand Down
18 changes: 9 additions & 9 deletions website_sale_product_assortment/controllers/variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ def get_info_assortment_preview(self, product_template_ids, **kw):
"""
res = []
templates = request.env["product.template"].sudo().browse(product_template_ids)
not_allowed_product_dict = templates.get_product_assortment_restriction_info(
templates.mapped("product_variant_ids.id")
(
assortments,
restricted_product_ids,
) = templates.get_product_assortment_restriction_info(
templates.product_variant_ids.ids
)
if not assortments:
return res
for template in templates:
variant_ids = set(template.product_variant_ids.ids)
if (
variant_ids
and variant_ids & set(not_allowed_product_dict.keys()) == variant_ids
):
if variant_ids and variant_ids & restricted_product_ids == variant_ids:
res.append(
{
"id": template.id,
"message_unavailable": not_allowed_product_dict[
variant_ids.pop()
][0].message_unavailable
"message_unavailable": assortments[0].message_unavailable
or _("Not available"),
}
)
Expand Down
59 changes: 36 additions & 23 deletions website_sale_product_assortment/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ def get_product_assortment_restriction_info(self, product_ids):
]
)
)
assortment_dict = {}
for assortment in assortments:
if partner & assortment.with_context(active_test=False).all_partner_ids:
allowed_product_ids = assortment.all_product_ids.ids
for product in product_ids:
if product not in allowed_product_ids:
assortment_dict.setdefault(product, self.env["ir.filters"])
assortment_dict[product] |= assortment
return assortment_dict
if not assortments:
return assortments, []
assortments = assortments.filtered(
lambda x: partner & x.with_context(active_test=False).all_partner_ids
)
# if there are multiple assortments for the same partner
# the allowed products are all the products present in
# at least one assortment (OR logic)
allowed_product_ids = assortments.all_product_ids.ids
restricted_product_ids = {
x for x in product_ids if x not in allowed_product_ids
}
return assortments, restricted_product_ids

def _get_combination_info(
self,
Expand All @@ -52,21 +56,30 @@ def _get_combination_info(
)
product_res_id = res["product_id"]
if self.env.context.get("website_id") and not only_template and product_res_id:
not_allowed_product_dict = self.get_product_assortment_restriction_info(
[product_res_id]
)
if not_allowed_product_dict and product_res_id in not_allowed_product_dict:
(
assortments,
restricted_product_ids,
) = self.get_product_assortment_restriction_info([product_res_id])
if restricted_product_ids and product_res_id in restricted_product_ids:
res["product_avoid_purchase"] = True
res["product_assortment_type"] = "no_purchase"
assortments = not_allowed_product_dict[product_res_id]
for assortment in assortments:
if assortment.website_availability == "no_show":
res["product_assortment_type"] = "no_show"
break
if res["product_assortment_type"] != "no_show":
assortment = assortments[0]
res["message_unavailable"] = assortment.message_unavailable
res["assortment_information"] = assortment.assortment_information

if any(a.website_availability == "no_show" for a in assortments):
res["product_assortment_type"] = "no_show"
else:
res["product_assortment_type"] = "no_purchase"

asrtm = assortments.filtered("message_unavailable")
if asrtm:
res["message_unavailable"] = asrtm[0].message_unavailable
else:
res["message_unavailable"] = ""

asrtm = assortments.filtered("assortment_information")
if asrtm:
res["assortment_information"] = asrtm[0].assortment_information
else:
res["assortment_information"] = ""

else:
res["product_avoid_purchase"] = False
return res
3 changes: 3 additions & 0 deletions website_sale_product_assortment/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

* `Ooops <https://www.ooops404.com>`_:
* Ashish Hirpara (https://ashish-hirpara.com)

* `PyTech SRL <https://www.pytech.it>`_:
* Alessandro Uffreduzzi
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright 2021 Tecnativa - Carlos Roca
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define("website_sale_product_assortment.tour_no_show_multifilter", function (
require
) {
"use strict";

var tour = require("web_tour.tour");
var base = require("web_editor.base");

var steps = [
{
trigger: "a[href='/shop']",
extra_trigger:
".o_wsale_product_grid_wrapper:has(a:contains('Test Product 1'))",
},
{
trigger: "a[href='/shop']",
extra_trigger:
".o_wsale_product_grid_wrapper:has(a:contains('Test Product 2'))",
},
];

tour.register(
"test_assortment_with_no_show_multifilter",
{
url: "/shop",
test: true,
wait_for: base.ready(),
},
steps
);
return {
steps: steps,
};
});
4 changes: 4 additions & 0 deletions website_sale_product_assortment/templates/assets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
type="text/javascript"
src="/website_sale_product_assortment/static/src/js/no_show_tour.js"
/>
<script
type="text/javascript"
src="/website_sale_product_assortment/static/src/js/no_show_multifilter_tour.js"
/>
</xpath>
</template>
</odoo>
38 changes: 37 additions & 1 deletion website_sale_product_assortment/tests/test_ui.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright 2021 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.tests.common import HttpCase
from odoo.tests import HttpCase, tagged


@tagged("post_install", "-at_install")
class TestUI(HttpCase):
def setUp(self):
super().setUp()
Expand All @@ -14,6 +15,14 @@ def setUp(self):
"type": "consu",
}
)
self.product_service = self.env["product.template"].create(
{
"name": "Test Product 2",
"is_published": True,
"website_sequence": 1,
"type": "service",
}
)

def test_01_ui_no_restriction(self):
self.env["ir.filters"].create(
Expand Down Expand Up @@ -60,3 +69,30 @@ def test_03_ui_no_purchase(self):
}
)
self.start_tour("/shop", "test_assortment_with_no_purchase", login="admin")

def test_04_ui_no_show_multifilter(self):
self.env["ir.filters"].create(
{
"name": "Assortment Service",
"model_id": "product.product",
"is_assortment": True,
"domain": [("type", "=", "service")],
"partner_domain": "[('id', '=', %s)]"
% self.env.ref("base.partner_admin").id,
"website_availability": "no_show",
}
)
self.env["ir.filters"].create(
{
"name": "Assortment Consu",
"model_id": "product.product",
"is_assortment": True,
"domain": [("type", "=", "consu")],
"partner_domain": "[('id', '=', %s)]"
% self.env.ref("base.partner_admin").id,
"website_availability": "no_show",
}
)
self.start_tour(
"/shop", "test_assortment_with_no_show_multifilter", login="admin"
)

0 comments on commit b84e956

Please sign in to comment.