diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 1b219a179e3..24c3c44bd44 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -407,13 +407,20 @@ class ProductTemplate(models.Model): Product = self.env['product.product'] templates = self.browse([]) + domain_no_variant = [('product_variant_ids', '=', False)] while True: domain = templates and [('product_tmpl_id', 'not in', templates.ids)] or [] args = args if args is not None else [] products_ns = Product._name_search(name, args+domain, operator=operator, name_get_uid=name_get_uid) products = Product.browse([x[0] for x in products_ns]) templates |= products.mapped('product_tmpl_id') - if (not products) or (limit and (len(templates) > limit)): + current_round_templates = self.browse([]) + if not products: + domain_template = args + domain_no_variant + (templates and [('id', 'not in', templates.ids)] or []) + template_ns = super(ProductTemplate, self)._name_search(name=name, args=domain_template, operator=operator, limit=limit, name_get_uid=name_get_uid) + current_round_templates |= self.browse([ns[0] for ns in template_ns]) + templates |= current_round_templates + if (not products and not current_round_templates) or (limit and (len(templates) > limit)): break # re-apply product.template order + name_get diff --git a/addons/product/tests/test_variants.py b/addons/product/tests/test_variants.py index 251996a2526..97323a0cee3 100644 --- a/addons/product/tests/test_variants.py +++ b/addons/product/tests/test_variants.py @@ -817,3 +817,25 @@ class TestVariantsArchive(common.TestProductCommon): "We should have re-activated one variant only.") Product._revert_method('unlink') + + def test_name_search_dynamic_attributes(self): + dynamic_attr = self.env['product.attribute'].create({ + 'name': 'Dynamic', + 'create_variant': 'dynamic', + 'value_ids': [(0, False, {'name': 'ValueDynamic'})], + }) + template = self.env['product.template'].create({ + 'name': 'cimanyd' + }) + self.assertEqual(len(template.product_variant_ids), 1) + + template.write({ + 'attribute_line_ids': [(0, False, { + 'attribute_id': dynamic_attr.id, + 'value_ids': [(4, dynamic_attr.value_ids[0].id, False)], + })] + }) + self.assertEqual(len(template.product_variant_ids), 0) + + name_searched = self.env['product.template'].name_search(name='cima') + self.assertIn(template.id, [ng[0] for ng in name_searched])