74 lines
2.9 KiB
Python
74 lines
2.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
import logging
|
|
from odoo import models, fields, api, _
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
class ProductTemplate(models.Model):
|
|
_inherit = 'product.template'
|
|
|
|
meta_sync_enabled = fields.Boolean(
|
|
string='Sync to Meta Catalog',
|
|
default=True,
|
|
help='Include this product in Meta Commerce and Instagram Shopping synchronization.'
|
|
)
|
|
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):
|
|
"""Manual sync action from the product template form or tree view."""
|
|
queue_model = self.env['social.meta.sync.queue']
|
|
for tmpl in self:
|
|
for variant in tmpl.product_variant_ids:
|
|
queue_model.enqueue_product(variant, operation='create_update')
|
|
|
|
# Trigger queue processing immediately
|
|
queue_model.process_queue(limit=50)
|
|
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'display_notification',
|
|
'params': {
|
|
'title': _("Meta Sync Triggered"),
|
|
'message': _("Product has been enqueued and synchronization initiated."),
|
|
'type': 'info',
|
|
'sticky': False,
|
|
}
|
|
}
|
|
|
|
def write(self, vals):
|
|
res = super().write(vals)
|
|
sync_trigger_fields = {
|
|
'name', 'list_price', 'is_published', 'active', 'image_1920',
|
|
'description_sale', 'categ_id', 'product_template_image_ids',
|
|
'meta_sync_enabled'
|
|
}
|
|
if any(f in vals for f in sync_trigger_fields):
|
|
try:
|
|
queue_model = self.env['social.meta.sync.queue']
|
|
for tmpl in self:
|
|
if not tmpl.meta_sync_enabled:
|
|
continue
|
|
operation = 'delete' if (vals.get('is_published') is False or vals.get('active') is False) else 'create_update'
|
|
for variant in tmpl.product_variant_ids:
|
|
queue_model.enqueue_product(variant, operation=operation)
|
|
except Exception as e:
|
|
_logger.warning("Non-blocking error enqueuing Meta sync for template: %s", e)
|
|
return res
|
|
|
|
def unlink(self):
|
|
for tmpl in self:
|
|
try:
|
|
queue_model = self.env['social.meta.sync.queue']
|
|
for variant in tmpl.product_variant_ids:
|
|
queue_model.enqueue_product(variant, operation='delete')
|
|
except Exception as e:
|
|
_logger.warning("Non-blocking error enqueuing Meta deletion for template %s: %s", tmpl.id, e)
|
|
return super().unlink()
|