63 lines
2.5 KiB
Python
63 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
import logging
|
|
from odoo import models, fields, api, _
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
class ProductProduct(models.Model):
|
|
_inherit = 'product.product'
|
|
|
|
meta_product_id = fields.Char(string='Meta Retailer ID', copy=False,
|
|
help='Unique ID / SKU used in Meta Commerce Catalog.')
|
|
meta_sync_status = fields.Selection([
|
|
('pending', 'Pending Sync'),
|
|
('synced', 'Synced'),
|
|
('failed', 'Sync Error'),
|
|
('excluded', 'Excluded')
|
|
], string='Meta Sync Status', default='pending', copy=False)
|
|
meta_last_sync = fields.Datetime(string='Meta Last Synced', readonly=True, copy=False)
|
|
meta_sync_error = fields.Text(string='Meta Sync Error', readonly=True, copy=False)
|
|
|
|
def action_sync_to_meta(self):
|
|
queue_model = self.env['social.meta.sync.queue']
|
|
for product in self:
|
|
queue_model.enqueue_product(product, operation='create_update')
|
|
queue_model.process_queue(limit=50)
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'display_notification',
|
|
'params': {
|
|
'title': _("Meta Sync Triggered"),
|
|
'message': _("Product variant enqueued for Meta catalog synchronization."),
|
|
'type': 'info',
|
|
'sticky': False,
|
|
}
|
|
}
|
|
|
|
def write(self, vals):
|
|
res = super().write(vals)
|
|
sync_trigger_fields = {
|
|
'lst_price', 'default_code', 'active', 'image_variant_1920',
|
|
'product_template_attribute_value_ids'
|
|
}
|
|
if any(f in vals for f in sync_trigger_fields):
|
|
try:
|
|
queue_model = self.env['social.meta.sync.queue']
|
|
for product in self:
|
|
if not product.product_tmpl_id.meta_sync_enabled:
|
|
continue
|
|
operation = 'delete' if vals.get('active') is False else 'create_update'
|
|
queue_model.enqueue_product(product, operation=operation)
|
|
except Exception as e:
|
|
_logger.warning("Non-blocking error enqueuing Meta sync for variant: %s", e)
|
|
return res
|
|
|
|
def unlink(self):
|
|
for product in self:
|
|
try:
|
|
queue_model = self.env['social.meta.sync.queue']
|
|
queue_model.enqueue_product(product, operation='delete')
|
|
except Exception as e:
|
|
_logger.warning("Non-blocking error enqueuing Meta deletion for variant %s: %s", product.id, e)
|
|
return super().unlink()
|