Skip to content

Commit 28103a5

Browse files
committed
[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 odoo#13757
1 parent e81f186 commit 28103a5

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

addons/product/models/product_template.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -368,21 +368,20 @@ def _price_get(self, products, ptype='list_price'):
368368
@api.multi
369369
def create_variant_ids(self):
370370
Product = self.env["product.product"]
371-
372371
for tmpl_id in self.with_context(active_test=False):
373-
# list of values combination
374-
existing_variants = [set(variant.attribute_value_ids.ids) for variant in tmpl_id.product_variant_ids]
375-
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))
376-
variant_matrix = map(lambda record_list: reduce(lambda x, y: x+y, record_list, self.env['product.attribute.value']), variant_matrix)
377-
to_create_variants = filter(lambda rec_set: set(rec_set.ids) not in existing_variants, variant_matrix)
378-
379372
# adding an attribute with only one value should not recreate product
380373
# write this attribute on every product to make sure we don't lose them
381374
variant_alone = tmpl_id.attribute_line_ids.filtered(lambda line: len(line.value_ids) == 1).mapped('value_ids')
382375
for value_id in variant_alone:
383376
updated_products = tmpl_id.product_variant_ids.filtered(lambda product: value_id.attribute_id not in product.mapped('attribute_value_ids.attribute_id'))
384377
updated_products.write({'attribute_value_ids': [(4, value_id.id)]})
385378

379+
# list of values combination
380+
existing_variants = [set(variant.attribute_value_ids.ids) for variant in tmpl_id.product_variant_ids]
381+
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))
382+
variant_matrix = map(lambda record_list: reduce(lambda x, y: x+y, record_list, self.env['product.attribute.value']), variant_matrix)
383+
to_create_variants = filter(lambda rec_set: set(rec_set.ids) not in existing_variants, variant_matrix)
384+
386385
# check product
387386
variants_to_activate = self.env['product.product']
388387
variants_to_unlink = self.env['product.product']

0 commit comments

Comments
 (0)