From c76734655e349b2b6c12d8462d57400ecbfcd64a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Warnon?= Date: Tue, 2 Oct 2018 09:55:43 +0000 Subject: [PATCH] [FIX] (website_)sale: handle archived variants - product configurator Targets commit d3530eb07e24278117ab396ecb86a59deecc312d Purpose ======= - When a product variant is archived by the user, it should not be selectable in the product configurator for both webshop and sale order - Fixed a typo in the name_get method of ProductTemplateAttributeValue closes odoo/odoo#27385 --- addons/product/models/product_attribute.py | 2 +- .../sale/controllers/product_configurator.py | 19 ++++++++++++++++++- .../src/js/product_configurator_mixin.js | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/addons/product/models/product_attribute.py b/addons/product/models/product_attribute.py index fb65cb09e64..5bc12588c23 100644 --- a/addons/product/models/product_attribute.py +++ b/addons/product/models/product_attribute.py @@ -187,7 +187,7 @@ class ProductTemplateAttributeValue(models.Model): @api.multi def name_get(self): if not self._context.get('show_attribute', True): # TDE FIXME: not used - return super(ProductAttributeValue, self).name_get() + return super(ProductTemplateAttributeValue, self).name_get() return [(value.id, "%s: %s" % (value.attribute_id.name, value.name)) for value in self] diff --git a/addons/sale/controllers/product_configurator.py b/addons/sale/controllers/product_configurator.py index b23e7ee9673..49572b64311 100644 --- a/addons/sale/controllers/product_configurator.py +++ b/addons/sale/controllers/product_configurator.py @@ -125,9 +125,26 @@ class ProductConfiguratorController(http.Controller): lambda filter_line: filter_line.product_tmpl_id == product ) for value_id in filter_line.value_ids.ids] + # Query all archived products for this template + archived_combinations = request.env['product.product'].search( + [('product_tmpl_id', '=', product.id), ('active', '=', False)]) + + if archived_combinations: + # Old archived variants could have a different set of attributes and are not relevant here + # -> filter them out + attribute_ids = product_attribute_values.mapped('attribute_id') + archived_combinations = archived_combinations.filtered( + lambda product: all( + attribute_id in product.mapped('product_template_attribute_value_ids.attribute_id') + for attribute_id in attribute_ids + ) + ) + return { 'exclusions': mapped_exclusions, - 'parent_exclusions': parent_exclusions + 'parent_exclusions': parent_exclusions, + 'archived_combinations': [archived_combination.product_template_attribute_value_ids.ids + for archived_combination in archived_combinations] } def _get_product_context(self, pricelist=None, **kw): diff --git a/addons/sale/static/src/js/product_configurator_mixin.js b/addons/sale/static/src/js/product_configurator_mixin.js index 25ee4adf80e..5908e151155 100644 --- a/addons/sale/static/src/js/product_configurator_mixin.js +++ b/addons/sale/static/src/js/product_configurator_mixin.js @@ -327,6 +327,9 @@ var ProductConfiguratorMixin = { * the exclusions coming from the parent product (meaning that this product * is an option of the parent product) * + * It will also check that the selected combination does not exactly + * match a manually archived product + * * @private * @param {$.Element} $parent the parent container to apply exclusions * @param {Array} combination the selected combination of product attribute values @@ -365,6 +368,18 @@ var ProductConfiguratorMixin = { }); } + if (combinationData.archived_combinations){ + _.each(combinationData.archived_combinations, function (archived_combination){ + if (disable) { + return; + } + + disable = _.every(archived_combination, function (attribute_value){ + return combination.indexOf(attribute_value) > -1; + }); + }); + } + $parent.toggleClass('css_not_available', disable); $parent.find("#add_to_cart").toggleClass('disabled', disable); $parent