[FIX] website_sale: fix ecommerce product variant image

On the e-commerce app, you can add product variants.
Those variants can have extra-image.

Before this commit:

    - The variants extra image are displayed on every product of the
      same type and they are duplicated on the product variant.

After this commit:

    - They are displayed only on the concerned product variant,
      without duplication.

OPW-2088261

closes odoo/odoo#39502

X-original-commit: 8efaa086db271cde792da77bca7a95a0d593df4d
Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
This commit is contained in:
jvm-odoo
2019-10-29 10:55:17 +00:00
committed by fw-bot
parent 5210929b37
commit c22b5a7f4d
2 changed files with 47 additions and 0 deletions
@@ -41,3 +41,23 @@ class ProductImage(models.Model):
for image in self:
if image.video_url and not image.embed_code:
raise ValidationError(_("Provided video URL for '%s' is not valid. Please enter a valid video URL.") % image.name)
@api.model_create_multi
def create(self, vals_list):
"""
We don't want the default_product_tmpl_id from the context
to be applied if we have a product_variant_id set to avoid
having the variant images to show also as template images.
But we want it if we don't have a product_variant_id set.
"""
context_without_template = self.with_context({k: v for k, v in self.env.context.items() if k != 'default_product_tmpl_id'})
normal_vals = []
variant_vals_list = []
for vals in vals_list:
if vals.get('product_variant_id') and 'default_product_tmpl_id' in self.env.context:
variant_vals_list.append(vals)
else:
normal_vals.append(vals)
return super().create(normal_vals) + super(ProductImage, context_without_template).create(variant_vals_list)
@@ -229,3 +229,30 @@ class TestWebsiteSaleImage(odoo.tests.HttpCase):
self.assertEqual(images[1].image_1920, red_image)
self.assertEqual(images[2].image_1920, image_gif)
self.assertEqual(images[3].image_1920, image_svg)
# CASE: When uploading a product variant image
# we don't want the default_product_tmpl_id from the context to be applied if we have a product_variant_id set
# we want the default_product_tmpl_id from the context to be applied if we don't have a product_variant_id set
additionnal_context = {'default_product_tmpl_id': template.id}
product = self.env['product.product'].create({
'product_tmpl_id': template.id,
})
product_image = self.env['product.image'].with_context(**additionnal_context).create([{
'name': 'Template image',
'image_1920': red_image,
}, {
'name': 'Variant image',
'image_1920': blue_image,
'product_variant_id': product.id,
}])
template_image = product_image.filtered(lambda i: i.name == 'Template image')
variant_image = product_image.filtered(lambda i: i.name == 'Variant image')
self.assertEqual(template_image.product_tmpl_id.id, template.id)
self.assertFalse(template_image.product_variant_id.id)
self.assertFalse(variant_image.product_tmpl_id.id)
self.assertEqual(variant_image.product_variant_id.id, product.id)