From 28103a5f2c30ad4398cbac2a1fba708042841e7f Mon Sep 17 00:00:00 2001 From: Jeremy Kersten Date: Wed, 12 Oct 2016 11:53:20 +0200 Subject: [PATCH] [FIX] product: fix duplicated creation variants Move the code to manage the case of 'adding an attribute with only one value should not recreate product' before to select the existing variants. Before this commit, When you create 1 new product.template, it create 1 variant. When you add an attribute with only one value, it update the existing and create a duplicate variant. After this commit, Only the existing variant is updated, and there are no more duplicated variant. The fix: We update the variant if only one value before to select the existing variant. Thus, the updated variant is compared with the matrix and not duplicated. This commit closes #13757 --- addons/product/models/product_template.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 437c193446b..2b2508cce9d 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -368,14 +368,7 @@ class ProductTemplate(models.Model): @api.multi def create_variant_ids(self): Product = self.env["product.product"] - for tmpl_id in self.with_context(active_test=False): - # list of values combination - existing_variants = [set(variant.attribute_value_ids.ids) for variant in tmpl_id.product_variant_ids] - variant_matrix = itertools.product(*(line.value_ids for line in tmpl_id.attribute_line_ids if line.value_ids and line.value_ids[0].attribute_id.create_variant)) - variant_matrix = map(lambda record_list: reduce(lambda x, y: x+y, record_list, self.env['product.attribute.value']), variant_matrix) - to_create_variants = filter(lambda rec_set: set(rec_set.ids) not in existing_variants, variant_matrix) - # adding an attribute with only one value should not recreate product # write this attribute on every product to make sure we don't lose them variant_alone = tmpl_id.attribute_line_ids.filtered(lambda line: len(line.value_ids) == 1).mapped('value_ids') @@ -383,6 +376,12 @@ class ProductTemplate(models.Model): updated_products = tmpl_id.product_variant_ids.filtered(lambda product: value_id.attribute_id not in product.mapped('attribute_value_ids.attribute_id')) updated_products.write({'attribute_value_ids': [(4, value_id.id)]}) + # list of values combination + existing_variants = [set(variant.attribute_value_ids.ids) for variant in tmpl_id.product_variant_ids] + variant_matrix = itertools.product(*(line.value_ids for line in tmpl_id.attribute_line_ids if line.value_ids and line.value_ids[0].attribute_id.create_variant)) + variant_matrix = map(lambda record_list: reduce(lambda x, y: x+y, record_list, self.env['product.attribute.value']), variant_matrix) + to_create_variants = filter(lambda rec_set: set(rec_set.ids) not in existing_variants, variant_matrix) + # check product variants_to_activate = self.env['product.product'] variants_to_unlink = self.env['product.product']