20% OFF UNTIL THE END OF THE YEAR Use Code: OFF20 for 20% Off!
How to extend your connector to add custom complex fields to synchronize for products?

How to extend your connector to add custom complex fields to synchronize for products?

Below is an example based on our Woocommerce connector. But all the connectors are extended the same way. First of all usually we name extension modules like “integration_woocommerce_extension

You need to extend all send and receive fields for product.product and product.template

integration_woocommerce_extension/models/fields/send_fields_product_template.py

from odoo.addons.integration_woocommerce.models.fields.send_fields_product_template import (
    SendFieldsProductTemplateWooCommerce,
)

class SendProductTemplateExtension(SendFieldsProductTemplateWooCommerce):

    def send_xxx(self, field_name):
        ...
        return {
            field_name: ...,
        }

integration_woocommerce_extension/models/fields/receive_fields_product_template.py

 from odoo.addons.integration_woocommerce.models.fields.receive_fields_product_template \
    import ReceiveFieldsProductTemplateWooCommerce


class ReceiveProductTemplateExtension(ReceiveFieldsProductTemplateWooCommerce):
    def receive_xxx(self, field_name):
        return {}

integration_woocommerce_extension/models/fields/send_fields_product_product.py

from odoo.addons.integration_woocommerce.models.fields.send_fields_product_product \
    import SendFieldsProductProductWooCommerce


class SendProductProductExtension(SendFieldsProductProductWooCommerce):
    pass

integration_woocommerce_extension/models/fields/receive_fields_product_product.py

from odoo.addons.integration_woocommerce.models.fields.receive_fields_product_product \
    import ReceiveFieldsProductProductWooCommerce


class ReceiveProductProductExtension(ReceiveFieldsProductProductWooCommerce):
    pass

Also if you want to add more API calls to Woocommerce, create also new API class

integration_woocommerce_extension/woocommerce_api_client.py

from odoo.addons.integration_woocommerce.woocommerce_api_client import WooCommerceApiClient

class WooCommerceApiClientExtension(WooCommerceApiClient):
  pass

Now register all new classes in sales integration

integration_woocommerce_extension/models/sale_integration.py

from .fields.send_fields_product_product import SendProductProductExtension
from .fields.send_fields_product_template import SendProductTemplateExtension

from .fields.receive_fields_product_product import ReceiveProductProductExtension
from .fields.receive_fields_product_template import ReceiveProductTemplateExtension

from ..woocommerce_api_client import WooCommerceApiClientExtension


class SaleIntegration(models.Model):
    _inherit = 'sale.integration'

    def init_send_field_converter(self, odoo_obj=False):
        if not self.is_woocommerce():
            return super(SaleIntegration, self).init_send_field_converter(odoo_obj)

        if getattr(odoo_obj, '_name', '') == 'product.template':
            return SendProductTemplateExtension(self, odoo_obj)
        if getattr(odoo_obj, '_name', '') == 'product.product':
            return SendProductProductExtension(self, odoo_obj)

        return super(SaleIntegration, self).init_send_field_converter(odoo_obj)

    def init_receive_field_converter(self, odoo_obj=False, external_obj=False):
        if not self.is_woocommerce():
            return super(SaleIntegration, self).init_receive_field_converter(odoo_obj, external_obj)

        if getattr(odoo_obj, '_name', '') == 'product.template':
            return ReceiveProductTemplateExtension(self, odoo_obj, external_obj)
        if getattr(odoo_obj, '_name', '') == 'product.product':
            return ReceiveProductProductExtension(self, odoo_obj, external_obj)

        return super(SaleIntegration, self).init_receive_field_converter(odoo_obj, external_obj)
        
    def get_class(self):
        self.ensure_one()
        if self.is_woocommerce():
            return WooCommerceApiClientExtension

        return super(SaleIntegration, self).get_class()        

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *