[IMP] website_sale: avoid pollute urls with #attrs if no variant

closes odoo/odoo#159474

Signed-off-by: Valentin Chevalier <vcr@odoo.com>
This commit is contained in:
Jeremy Kersten
2024-03-27 17:45:01 +00:00
committed by Valentin Chevalier
parent 6101700868
commit aa09df2584
2 changed files with 9 additions and 1 deletions
@@ -1,5 +1,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from werkzeug.urls import url_join
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
@@ -46,7 +48,10 @@ class Product(models.Model):
def _compute_product_website_url(self):
for product in self:
attributes = ','.join(str(x) for x in product.product_template_attribute_value_ids.ids)
product.website_url = "%s#attr=%s" % (product.product_tmpl_id.website_url, attributes)
url = product.product_tmpl_id.website_url
if attributes:
url = url_join(url, f"#attr={attributes}")
product.website_url = url
def _prepare_variant_values(self, combination):
variant_dict = super()._prepare_variant_values(combination)
@@ -185,6 +185,9 @@ export const WebsiteSale = publicWidget.Widget.extend(VariantMixin, cartHandlerM
*/
_setUrlHash: function ($parent) {
var $attributes = $parent.find('input.js_variant_change:checked, select.js_variant_change option:selected');
if (!$attributes.length) {
return;
}
var attributeIds = $attributes.toArray().map((elem) => $(elem).data("value_id"));
window.location.replace('#attr=' + attributeIds.join(','));
},