from odoo import fields, models


class MailchimpMergeFields(models.Model):
    _inherit = "mailchimp.merge.fields"

    # Allows to use fields from mail.mass_mailing.contact records in merge fields
    field_id = fields.Many2one(domain=[
        ('model_id.model', 'in', ['res.partner', 'mail.mass_mailing.contact']),
        ('ttype', 'not in', ['one2many', 'many2one', 'many2many'])]
    )

    def get_value(self, mailing_contact):
        contact_fields = self.filtered(
            lambda f: f.field_id.model == "mail.mass_mailing.contact")
        partner_fields = self - contact_fields
        res = super(MailchimpMergeFields, partner_fields).get_value(mailing_contact)
        for custom_field in contact_fields:
            if custom_field.field_id and hasattr(
                    self, custom_field.field_id.name):
                value = getattr(self, custom_field.field_id.name)
            else:
                value = ''
            res.update({custom_field.tag: value or ''})
        return res

from odoo import fields, models


class MailchimpMergeFields(models.Model):
    _inherit = "mailchimp.merge.fields"

    # Allows to use fields from mail.mass_mailing.contact records in merge fields
    field_id = fields.Many2one(domain=[
        ('model_id.model', 'in', ['res.partner', 'mail.mass_mailing.contact']),
        ('ttype', 'not in', ['one2many', 'many2one', 'many2many'])]
    )

    def get_value(self, mailing_contact):
        contact_fields = self.filtered(
            lambda f: f.field_id.model == "mail.mass_mailing.contact")
        partner_fields = self - contact_fields
        res = super(MailchimpMergeFields, partner_fields).get_value(mailing_contact)
        for custom_field in contact_fields:
            if custom_field.field_id and hasattr(
                    mailing_contact, custom_field.field_id.name):
                value = getattr(mailing_contact, custom_field.field_id.name)
            else:
                value = ''
            res.update({custom_field.tag: value or ''})
        return res

FIX mailing_contact merge fields value
FIX mailing_contact merge fields value

  from odoo import fields, models
  
  
  class MailchimpMergeFields(models.Model):
      _inherit = "mailchimp.merge.fields"
  
      # Allows to use fields from mail.mass_mailing.contact records in merge fields
      field_id = fields.Many2one(domain=[
          ('model_id.model', 'in', ['res.partner', 'mail.mass_mailing.contact']),
          ('ttype', 'not in', ['one2many', 'many2one', 'many2many'])]
      )
  
      def get_value(self, mailing_contact):
          contact_fields = self.filtered(
              lambda f: f.field_id.model == "mail.mass_mailing.contact")
          partner_fields = self - contact_fields
          res = super(MailchimpMergeFields, partner_fields).get_value(mailing_contact)
          for custom_field in contact_fields:
              if custom_field.field_id and hasattr(
-                     self, custom_field.field_id.name):
+                     mailing_contact, custom_field.field_id.name):
-                 value = getattr(self, custom_field.field_id.name)
+                 value = getattr(mailing_contact, custom_field.field_id.name)
              else:
                  value = ''
              res.update({custom_field.tag: value or ''})
          return res
  
FIX mailing_contact merge fields value
## Code Before:
from odoo import fields, models


class MailchimpMergeFields(models.Model):
    _inherit = "mailchimp.merge.fields"

    # Allows to use fields from mail.mass_mailing.contact records in merge fields
    field_id = fields.Many2one(domain=[
        ('model_id.model', 'in', ['res.partner', 'mail.mass_mailing.contact']),
        ('ttype', 'not in', ['one2many', 'many2one', 'many2many'])]
    )

    def get_value(self, mailing_contact):
        contact_fields = self.filtered(
            lambda f: f.field_id.model == "mail.mass_mailing.contact")
        partner_fields = self - contact_fields
        res = super(MailchimpMergeFields, partner_fields).get_value(mailing_contact)
        for custom_field in contact_fields:
            if custom_field.field_id and hasattr(
                    self, custom_field.field_id.name):
                value = getattr(self, custom_field.field_id.name)
            else:
                value = ''
            res.update({custom_field.tag: value or ''})
        return res

## Instruction:
FIX mailing_contact merge fields value
## Code After:
from odoo import fields, models


class MailchimpMergeFields(models.Model):
    _inherit = "mailchimp.merge.fields"

    # Allows to use fields from mail.mass_mailing.contact records in merge fields
    field_id = fields.Many2one(domain=[
        ('model_id.model', 'in', ['res.partner', 'mail.mass_mailing.contact']),
        ('ttype', 'not in', ['one2many', 'many2one', 'many2many'])]
    )

    def get_value(self, mailing_contact):
        contact_fields = self.filtered(
            lambda f: f.field_id.model == "mail.mass_mailing.contact")
        partner_fields = self - contact_fields
        res = super(MailchimpMergeFields, partner_fields).get_value(mailing_contact)
        for custom_field in contact_fields:
            if custom_field.field_id and hasattr(
                    mailing_contact, custom_field.field_id.name):
                value = getattr(mailing_contact, custom_field.field_id.name)
            else:
                value = ''
            res.update({custom_field.tag: value or ''})
        return res
