# -*- coding: utf-8 -*- from odoo import models, fields, api import urllib.parse class Website(models.Model): _inherit = 'website' # Meta Catalog configuration per website meta_catalog_id = fields.Char(string='Meta Catalog ID', compute='_compute_meta_settings', inverse='_inverse_meta_settings', store=True) meta_sync_enabled = fields.Boolean(string='Meta Sync Enabled', compute='_compute_meta_settings', inverse='_inverse_meta_settings', store=True) # WhatsApp Business configuration per website whatsapp_enabled = fields.Boolean(string='WhatsApp Enabled', compute='_compute_whatsapp_settings', inverse='_inverse_whatsapp_settings', store=True) whatsapp_business_phone_number = fields.Char(string='WhatsApp Contact Phone', compute='_compute_whatsapp_settings', inverse='_inverse_whatsapp_settings', store=True) whatsapp_button_product_page = fields.Boolean(string='Product Page Button', compute='_compute_whatsapp_settings', inverse='_inverse_whatsapp_settings', store=True) whatsapp_button_floating = fields.Boolean(string='Floating Button', compute='_compute_whatsapp_settings', inverse='_inverse_whatsapp_settings', store=True) whatsapp_prefilled_message = fields.Text(string='WhatsApp Inquiry Template', compute='_compute_whatsapp_settings', inverse='_inverse_whatsapp_settings', store=True) # Specific website override flags has_custom_social_settings = fields.Boolean(string='Override Company Social Settings', default=False) custom_meta_catalog_id = fields.Char(string='Custom Meta Catalog ID') custom_meta_sync_enabled = fields.Boolean(string='Custom Meta Sync Enabled', default=False) custom_whatsapp_enabled = fields.Boolean(string='Custom WhatsApp Enabled', default=False) custom_whatsapp_phone = fields.Char(string='Custom WhatsApp Phone') custom_whatsapp_button_product = fields.Boolean(string='Custom Product Button', default=True) custom_whatsapp_button_floating = fields.Boolean(string='Custom Floating Button', default=True) custom_whatsapp_prefilled = fields.Text(string='Custom Inquiry Template', default="Hi, I'm interested in {product_name}.") @api.depends('company_id', 'has_custom_social_settings', 'company_id.meta_catalog_id', 'company_id.meta_sync_enabled', 'custom_meta_catalog_id', 'custom_meta_sync_enabled') def _compute_meta_settings(self): for website in self: if website.has_custom_social_settings: website.meta_catalog_id = website.custom_meta_catalog_id website.meta_sync_enabled = website.custom_meta_sync_enabled else: website.meta_catalog_id = website.company_id.meta_catalog_id website.meta_sync_enabled = website.company_id.meta_sync_enabled def _inverse_meta_settings(self): for website in self: if website.has_custom_social_settings: website.custom_meta_catalog_id = website.meta_catalog_id website.custom_meta_sync_enabled = website.meta_sync_enabled elif website.company_id: website.company_id.meta_catalog_id = website.meta_catalog_id website.company_id.meta_sync_enabled = website.meta_sync_enabled @api.depends('company_id', 'has_custom_social_settings', 'company_id.whatsapp_enabled', 'company_id.whatsapp_business_phone_number', 'company_id.whatsapp_button_product_page', 'company_id.whatsapp_button_floating', 'company_id.whatsapp_prefilled_message') def _compute_whatsapp_settings(self): for website in self: if website.has_custom_social_settings: website.whatsapp_enabled = website.custom_whatsapp_enabled website.whatsapp_business_phone_number = website.custom_whatsapp_phone website.whatsapp_button_product_page = website.custom_whatsapp_button_product website.whatsapp_button_floating = website.custom_whatsapp_button_floating website.whatsapp_prefilled_message = website.custom_whatsapp_prefilled else: website.whatsapp_enabled = website.company_id.whatsapp_enabled website.whatsapp_business_phone_number = website.company_id.whatsapp_business_phone_number website.whatsapp_button_product_page = website.company_id.whatsapp_button_product_page website.whatsapp_button_floating = website.company_id.whatsapp_button_floating website.whatsapp_prefilled_message = website.company_id.whatsapp_prefilled_message def _inverse_whatsapp_settings(self): for website in self: if website.has_custom_social_settings: website.custom_whatsapp_enabled = website.whatsapp_enabled website.custom_whatsapp_phone = website.whatsapp_business_phone_number website.custom_whatsapp_button_product = website.whatsapp_button_product_page website.custom_whatsapp_button_floating = website.whatsapp_button_floating website.custom_whatsapp_prefilled = website.whatsapp_prefilled_message elif website.company_id: website.company_id.whatsapp_enabled = website.whatsapp_enabled website.company_id.whatsapp_business_phone_number = website.whatsapp_business_phone_number website.company_id.whatsapp_button_product_page = website.whatsapp_button_product_page website.company_id.whatsapp_button_floating = website.whatsapp_button_floating website.company_id.whatsapp_prefilled_message = website.whatsapp_prefilled_message def clean_whatsapp_phone(self, phone=None): raw_phone = phone or self.whatsapp_business_phone_number or '' return ''.join(filter(str.isdigit, raw_phone)) def get_whatsapp_chat_url(self, product=None): self.ensure_one() phone = self.clean_whatsapp_phone() if not phone: return '#' template = self.whatsapp_prefilled_message or "Hi, I'm interested in {product_name}." if product: prod_name = product.name or '' sku = product.default_code or '' base_url = self.get_base_url() prod_url = f"{base_url}{product.website_url}" if hasattr(product, 'website_url') else '' message = template.replace('{product_name}', prod_name) message = message.replace('{sku}', sku) message = message.replace('{url}', prod_url) if '{url}' not in template and prod_url: message += f" ({prod_url})" else: message = "Hi, I have an enquiry regarding your products." return f"https://wa.me/{phone}?text={urllib.parse.quote(message)}"