Skip to content

Commit

Permalink
[MIG] website_sale_product_assortment: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PieterPaulussen committed Jul 23, 2024
1 parent 3c62c27 commit c8d915d
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 117 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ env:

# See https://github.com/OCA/odoo-community.org/issues/37#issuecomment-470686449
parserOptions:
ecmaVersion: 2022
ecmaVersion: 2020
sourceType: module

overrides:
- files:
Expand Down
4 changes: 2 additions & 2 deletions website_sale_product_assortment/controllers/variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
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 WebsiteSaleVariantControllerAssortment(WebsiteSaleVariantController):
@http.route(
["/sale/get_info_assortment_preview"],
type="json",
Expand Down
33 changes: 32 additions & 1 deletion website_sale_product_assortment/controllers/website_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

from odoo.http import request, route

from odoo.addons.website.controllers.main import Website
from odoo.addons.website_sale.controllers.main import WebsiteSale


class WebsiteSale(WebsiteSale):
class WebsiteSaleAssortment(WebsiteSale):
def _get_products_allowed(self):
partner = request.env.user.partner_id
website_id = request.website.id
Expand Down Expand Up @@ -76,3 +77,33 @@ def _get_search_options(
("product_variant_ids", "in", list(allowed_product_ids))
]
return res


class WebsiteAssortment(Website):
@route()
def autocomplete(
self,
search_type=None,
term=None,
order=None,
limit=5,
max_nb_chars=999,
options=None,
):
(
allowed_product_ids,
assortment_restriction,
) = WebsiteSaleAssortment._get_products_allowed(self)
if assortment_restriction:
options["allowed_product_domain"] = [
("product_variant_ids", "in", list(allowed_product_ids))
]

return super().autocomplete(
search_type=search_type,
term=term,
order=order,
limit=limit,
max_nb_chars=max_nb_chars,
options=options,
)
2 changes: 0 additions & 2 deletions website_sale_product_assortment/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ def _get_combination_info(
combination=False,
product_id=False,
add_qty=1,
pricelist=False,
parent_combination=False,
only_template=False,
):
res = super()._get_combination_info(
combination=combination,
product_id=product_id,
add_qty=add_qty,
pricelist=pricelist,
parent_combination=parent_combination,
only_template=only_template,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
odoo.define("website_sale_product_assortment.assortment_preview", function (require) {
"use strict";
/** @odoo-module **/
// Copyright 2021 Tecnativa - Carlos Roca
// License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

const publicWidget = require("web.public.widget");
const core = require("web.core");
import {jsonrpc} from "@web/core/network/rpc_service";
import publicWidget from "@web/legacy/js/public/public_widget";
import {renderToFragment} from "@web/core/utils/render";

publicWidget.registry.WebsiteSaleProductAssortment = publicWidget.Widget.extend({
selector: "#products_grid",
export const WebsiteSaleProductAssortment = publicWidget.Widget.extend({
selector: "#products_grid",

start: function () {
this._super.apply(this, arguments);
this.render_assortments();
},
render_assortments: 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_info_assortment_preview",
params: {product_template_ids: product_ids},
}).then((product_values) => {
for (const product of product_values) {
this.render_product_assortment(product_dic[product.id], product);
}
});
},
render_product_assortment: function (product_info, product) {
$(product_info)
.find(".product_price")
.append(
$(
core.qweb.render(
"website_sale_product_assortment.product_availability",
{
message_unavailable: product.message_unavailable,
product_template_id: product.id,
}
)
).get(0)
);
start: function () {
this._super.apply(this, arguments);
this.render_assortments();
},

$(product_info).find(".fa-shopping-cart").parent().addClass("disabled");
},
});
render_assortments: 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 jsonrpc("/sale/get_info_assortment_preview", {
product_template_ids: product_ids,
}).then((product_values) => {
for (const product of product_values) {
this.render_product_assortment(product_dic[product.id], product);
}
});
},

render_product_assortment: function (product_info, product) {
$(product_info)
.find(".product_price")
.append(
renderToFragment(
"website_sale_product_assortment.product_availability",
{
message_unavailable: product.message_unavailable,
product_template_id: product.id,
}
)
);
$(product_info).find(".fa-shopping-cart").parent().addClass("disabled");
},
});

publicWidget.registry.WebsiteSaleProductAssortment = WebsiteSaleProductAssortment;
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* 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_purchase", function (require) {
"use strict";

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

var steps = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* 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_restriction", function (require) {
"use strict";

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

var steps = [
Expand Down
2 changes: 0 additions & 2 deletions website_sale_product_assortment/static/src/js/no_show_tour.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* 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", function (require) {
"use strict";

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

var steps = [
Expand Down
107 changes: 47 additions & 60 deletions website_sale_product_assortment/static/src/js/variant_mixin.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,52 @@
/** @odoo-module **/
// Copyright 2021 Tecnativa - Carlos Roca
// License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
odoo.define("website_sale_product_assortment.VariantMixin", function (require) {
"use strict";

var VariantMixin = require("sale.VariantMixin");
var publicWidget = require("web.public.widget");
var core = require("web.core");
var QWeb = core.qweb;
import VariantMixin from "@website_sale/js/sale_variant_mixin";
import {renderToFragment} from "@web/core/utils/render";

require("website_sale.website_sale");
VariantMixin._onChangeCombinationAssortment = function (ev, $parent, combination) {
let product_id = 0;
if ($parent.find("input.product_id:checked").length) {
product_id = $parent.find("input.product_id:checked").val();
} else {
product_id = $parent.find(".product_id").val();
}
const isMainProduct =
combination.product_id &&
($parent.is(".js_main_product") || $parent.is(".main_product")) &&
combination.product_id === parseInt(product_id, 10);
if (!this.isWebsite || !isMainProduct) {
return;
}
$(".oe_website_sale")
.find("#message_unavailable_" + combination.product_template_id)
.remove();
$("#product_full_assortment_description").remove();
if (!combination.product_avoid_purchase) {
return;
}
$parent.find("#add_to_cart").addClass("disabled");
$parent.find("#buy_now").addClass("disabled");
$(".oe_website_sale")
.find("#product_option_block")
.prepend(
renderToFragment(
"website_sale_product_assortment.product_availability",
combination
)
);
if (combination.assortment_information) {
$("#product_detail").after(
"<div id='product_full_assortment_description'>" +
combination.assortment_information +
"</div>"
);
}
};

VariantMixin._onChangeCombinationAssortment = function (ev, $parent, combination) {
let product_id = 0;
if ($parent.find("input.product_id:checked").length) {
product_id = $parent.find("input.product_id:checked").val();
} else {
product_id = $parent.find(".product_id").val();
}
const isMainProduct =
combination.product_id &&
($parent.is(".js_main_product") || $parent.is(".main_product")) &&
combination.product_id === parseInt(product_id, 10);
if (!this.isWebsite || !isMainProduct) {
return;
}
$(".oe_website_sale")
.find("#message_unavailable_" + combination.product_template_id)
.remove();
$("#product_full_assortment_description").remove();
if (!combination.product_avoid_purchase) {
return;
}
$parent.find("#add_to_cart").addClass("disabled");
$parent.find("#buy_now").addClass("disabled");
$(".oe_website_sale")
.find("#product_option_block")
.prepend(
QWeb.render(
"website_sale_product_assortment.product_availability",
combination
)
);
if (combination.assortment_information) {
$("#product_detail").after(
"<div id='product_full_assortment_description'>" +
combination.assortment_information +
"</div>"
);
}
};

publicWidget.registry.WebsiteSale.include({
/**
* Adds the stock checking to the regular _onChangeCombination method
* @override
*/
_onChangeCombination: function () {
this._super.apply(this, arguments);
VariantMixin._onChangeCombinationAssortment.apply(this, arguments);
},
});

return VariantMixin;
});
const originalOnChangeCombination = VariantMixin._onChangeCombination;
VariantMixin._onChangeCombination = function (ev, $parent, combination) {
originalOnChangeCombination.apply(this, arguments);
VariantMixin._onChangeCombinationAssortment.apply(this, ev, $parent, combination);
};
4 changes: 2 additions & 2 deletions website_sale_product_assortment/views/ir_filters_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
<field name="website_availability" />
<field
name="message_unavailable"
attrs="{'invisible': [('website_availability', '!=', 'no_purchase')]}"
invisible="website_availability != 'no_purchase'"
/>
<field
name="assortment_information"
attrs="{'invisible': [('website_availability', '!=', 'no_purchase')]}"
invisible="website_availability != 'no_purchase'"
/>
</group>
</xpath>
Expand Down

0 comments on commit c8d915d

Please sign in to comment.