[FIX] sale_product_configurator: set value for no_variant attribute

Steps to reproduce:
1) Create product with an attribute that has several values and with an
attribute with 'create_mode = 'no_variant' and one value.
2) Create SO with this product, save
3) Open product configurator again and see the traceback

Reason: selected_attribute_value_id is not set.

After this commit: the attribure value is defined in get_values of
product configurator.

opw-3513685

X-original-commit: 8cf7d706f86e830670fb3c8fd799e6cacefe122c
Part-of: odoo/odoo#150653
This commit is contained in:
Valeriya(vchu)
2024-01-26 10:35:01 +00:00
parent 212b466adc
commit 23a79eeca8
3 changed files with 60 additions and 3 deletions
+10
View File
@@ -89,6 +89,16 @@ class ProductAttributesCommon(ProductCommon):
cls.color_attribute_green,
) = cls.color_attribute.value_ids
cls.no_variant_attribute = cls.env['product.attribute'].create(
{
'name': 'No variant',
'create_variant': 'no_variant',
'value_ids': [
Command.create({'name': 'extra'}),
]
})
cls.no_variant_attribute_extra = cls.no_variant_attribute.value_ids
class ProductVariantsCommon(ProductAttributesCommon):
@@ -44,7 +44,14 @@ class ProductConfiguratorController(Controller):
combination = request.env['product.template.attribute.value']
if ptav_ids:
combination = request.env['product.template.attribute.value'].browse(ptav_ids).filtered(lambda ptav: ptav.product_tmpl_id.id == product_template_id)
combination = request.env['product.template.attribute.value'].browse(ptav_ids).filtered(
lambda ptav: ptav.product_tmpl_id.id == product_template_id
)
# Set attributes that are in mode `no_variant` and have only one value
unconfigured_ptals = product_template.attribute_line_ids - combination.attribute_line_id
combination += unconfigured_ptals.filtered(lambda ptal: ptal.value_count == 1).mapped(
lambda ptal: ptal.product_template_value_ids[:1]
)
if not combination:
combination = product_template._get_first_possible_combination()
@@ -10,7 +10,7 @@ from odoo.addons.sale.tests.common import SaleCommon
@tagged('post_install', '-at_install')
class TestProductConfiguratorData(HttpCaseWithUserDemo, ProductVariantsCommon, SaleCommon):
def request_get_values(self, product_template):
def request_get_values(self, product_template, ptav_ids=None):
base_url = product_template.get_base_url()
response = self.opener.post(
url=base_url + '/sale_product_configurator/get_values',
@@ -23,7 +23,7 @@ class TestProductConfiguratorData(HttpCaseWithUserDemo, ProductVariantsCommon, S
'product_uom_id': None,
'company_id': None,
'pricelist_id': None,
'ptav_ids': None,
'ptav_ids': ptav_ids,
'only_main_product': False,
},
}
@@ -56,6 +56,31 @@ class TestProductConfiguratorData(HttpCaseWithUserDemo, ProductVariantsCommon, S
],
})
def create_product_template_with_attribute_no_variant(self):
return self.env['product.template'].create({
'name': 'Chair',
'categ_id': self.product_category.id,
'attribute_line_ids': [
Command.create({
'attribute_id': self.no_variant_attribute.id,
'value_ids': [
Command.set([
self.no_variant_attribute_extra.id
]),
],
}),
Command.create({
'attribute_id': self.color_attribute.id,
'value_ids': [
Command.set([
self.color_attribute_red.id,
self.color_attribute_blue.id,
])
],
}),
],
})
def test_dropped_value_isnt_shown(self):
self.assertEqual(len(self.product_template_sofa.product_variant_ids), 3)
@@ -155,6 +180,21 @@ class TestProductConfiguratorData(HttpCaseWithUserDemo, ProductVariantsCommon, S
self.assertFalse(str(ptav_with_exclusion.id) in result['products'][0]['exclusions'])
self.assertFalse(str(ptav_excluded.id) in result['products'][0]['exclusions'])
def test_ptal_values_set_for_no_variant_atribute(self):
'''
Test that selected_attribute_value_id is set for attribute with only one variant and
`create_variant`: `no_variant`.
'''
product_template = self.create_product_template_with_attribute_no_variant()
self.authenticate('demo', 'demo')
ptav_red = product_template.attribute_line_ids.product_template_value_ids.filtered(
lambda ptav: ptav.product_attribute_value_id == self.color_attribute_red
)
result = self.request_get_values(product_template, [ptav_red.id])
self.assertTrue(result['products'][0]['attribute_lines'][1]['selected_attribute_value_ids'])
@tagged('post_install', '-at_install')
class TestSaleProductVariants(ProductAttributesCommon, SaleCommon):