diff --git a/addons/product/models/product.py b/addons/product/models/product.py index 506d170a135..78ae19738a1 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -154,30 +154,33 @@ class ProductProduct(models.Model): for record in self: record.can_image_variant_1024_be_zoomed = record.image_variant_1920 and tools.is_image_size_above(record.image_variant_1920, record.image_variant_1024) + def _set_template_field(self, template_field, variant_field): + for record in self: + if ( + # We are trying to remove a field from the variant even though it is already + # not set on the variant, remove it from the template instead. + (not record[template_field] and not record[variant_field]) + # We are trying to add a field to the variant, but the template field is + # not set, write on the template instead. + or (record[template_field] and not record.product_tmpl_id[template_field]) + # There is only one variant, always write on the template. + or self.search_count([ + ('product_tmpl_id', '=', record.product_tmpl_id.id), + ('active', '=', True), + ]) <= 1 + ): + record[variant_field] = False + record.product_tmpl_id[template_field] = record[template_field] + else: + record[variant_field] = record[template_field] + def _compute_image_1920(self): """Get the image from the template if no image is set on the variant.""" for record in self: record.image_1920 = record.image_variant_1920 or record.product_tmpl_id.image_1920 def _set_image_1920(self): - for record in self: - if ( - # We are trying to remove an image even though it is already - # not set, remove it from the template instead. - not record.image_1920 and not record.image_variant_1920 or - # We are trying to add an image, but the template image is - # not set, write on the template instead. - record.image_1920 and not record.product_tmpl_id.image_1920 or - # There is only one variant, always write on the template. - self.search_count([ - ('product_tmpl_id', '=', record.product_tmpl_id.id), - ('active', '=', True), - ]) <= 1 - ): - record.image_variant_1920 = False - record.product_tmpl_id.image_1920 = record.image_1920 - else: - record.image_variant_1920 = record.image_1920 + return self._set_template_field('image_1920', 'image_variant_1920') def _compute_image_1024(self): """Get the image from the template if no image is set on the variant."""