[FIX] product: create variant

1. Create an attribute with create_variant boolean un-ticked (ie. don't
   create variants from these attributes). For example 'Test Attribute'
2. Create attribute values related to the above attribute. For example
   'Test 1', 'Test 2', etc
3. Create a product (product.template) and assign an attribute (eg.
   Color: black and white)
4. On one of the product variants, add one of the attribute values for
   the attribute we've created in step 2 to the attribute_value_ids
   field to allow for filtering by product attributes that do not create
   variants.
5. Go back to product template and add another attribute value to an
   attribute where create_variant is True (eg. Memory: 16 GB)
6. Our value from attribute_value_ids is gone, and the product has been
   deactivated

When attributes are set with create_variant is False, they are
obviously not used to create variants. Therefore, when we are looking
for existing variants, we should filter out these attributes.

Partially closes #18682
opw-762743
This commit is contained in:
Nicolas Martinelli
2017-08-14 11:10:43 +02:00
parent 8102698dfd
commit 5c2380e9f4
+3 -3
View File
@@ -379,7 +379,7 @@ class ProductTemplate(models.Model):
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]
existing_variants = [set(variant.attribute_value_ids.filtered(lambda r: r.attribute_id.create_variant).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)
@@ -388,9 +388,9 @@ class ProductTemplate(models.Model):
variants_to_activate = self.env['product.product']
variants_to_unlink = self.env['product.product']
for product_id in tmpl_id.product_variant_ids:
if not product_id.active and product_id.attribute_value_ids in variant_matrix:
if not product_id.active and product_id.attribute_value_ids.filtered(lambda r: r.attribute_id.create_variant) in variant_matrix:
variants_to_activate |= product_id
elif product_id.attribute_value_ids not in variant_matrix:
elif product_id.attribute_value_ids.filtered(lambda r: r.attribute_id.create_variant) not in variant_matrix:
variants_to_unlink |= product_id
if variants_to_activate:
variants_to_activate.write({'active': True})