[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
This commit is contained in:
Jeremy Kersten
2016-10-12 12:38:38 +02:00
parent e81f186ff8
commit 28103a5f2c
+6 -7
View File
@@ -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']