Skip to content

Commit

Permalink
[FIX] product: prevent error while unarchive product with dynamic var…
Browse files Browse the repository at this point in the history
…iant

Currently, an error is generated when unarchiving multiple products which have
dynamic attributes & variants.

Step to produce:

- Install a 'Sales' module.
- Navigate to the Sales / Products / Products, And create a product.
- Add a 'Product Name' and save a record, and then add dynamic
Attributes & Variants.
- Repeat the process described in the above step to create another product with
the same configuration as the first one.
-Go to list view of 'Product'. select both products that we made recently
and click on the Action button to archive both products.
- Click on 'Filters' to see the archive product, And select those products
to Unarchive.

ValueError: Expected singleton: product.template(39, 37, 40, 42, 38, 46, 34)

The issue occurs when unarchive multiple products that contain dynamic
attributes & variants. As a result, the system receives multiple product
templates at [1].

link[1]: https://github.com/odoo/odoo/blob/e5635c38810a745f00b106d7a075ae1553de50c7/addons/product/models/product_template.py#L745

To resolve the issue, replace a 'self' with 'tmpl_id' to get a single product
template record instead of multiple.

sentry-5222160918

closes odoo#163326

Signed-off-by: Meet Gandhi (mega) <mega@odoo.com>
  • Loading branch information
mega-odoo committed May 10, 2024
1 parent a5f5df6 commit 4d1e2a4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion addons/product/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ def _create_variant_ids(self):

else:
for variant in existing_variants.values():
is_combination_possible = self._is_combination_possible_by_config(
is_combination_possible = tmpl_id._is_combination_possible_by_config(
combination=variant.product_template_attribute_value_ids,
ignore_no_variant=True,
)
Expand Down
34 changes: 34 additions & 0 deletions addons/product/tests/test_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,40 @@ def test_update_variant_with_nocreate(self):
# no_variant attribute should not appear on the variant
self.assertNotIn(self.size_S, template.product_variant_ids.product_template_attribute_value_ids.product_attribute_value_id)

def test_unarchive_multiple_products_with_variants(self):
product_attribut = self.env['product.attribute'].create({
'name': 'Color',
'sequence': 1,
'create_variant': 'dynamic',
})
attr_value = self.env['product.attribute.value'].create({
'name': 'Blue',
'attribute_id': product_attribut.id,
'sequence': 1,
})
first_product = self.env['product.template'].create({
'name': 'Sofa',
'attribute_line_ids': [(0, 0, {
'attribute_id': product_attribut.id,
'value_ids': [(6, 0, [attr_value.id])],
})]
})
second_product = first_product.copy({
'product_variant_ids': [(0, 0, {
'name': 'Sofa',
})]
})

products = first_product + second_product
products.action_archive()
self.assertFalse(first_product.active)
self.assertFalse(second_product.active)
self.assertFalse(second_product.product_variant_ids)
products.action_unarchive()
# check products should be unarchived successfully.
self.assertTrue(first_product.active)
self.assertTrue(second_product.active)
self.assertTrue(second_product.product_variant_ids)

class TestVariantsManyAttributes(common.TestAttributesCommon):

Expand Down

0 comments on commit 4d1e2a4

Please sign in to comment.