diff --git a/temba/channels/types/whatsapp/type.py b/temba/channels/types/whatsapp/type.py index 822dd24c192..d8995e9e7e5 100644 --- a/temba/channels/types/whatsapp/type.py +++ b/temba/channels/types/whatsapp/type.py @@ -160,14 +160,13 @@ def get_api_catalogs(self, channel): HTTPLog.create_from_exception(HTTPLog.WHATSAPP_CATALOGS_SYNCED, url, e, start, channel=channel) return [], False - def get_api_products(self, channel, catalog): + def get_api_products(self, channel, facebook_catalog_id): if ( CONFIG_FB_BUSINESS_ID not in channel.config or CONFIG_FB_ACCESS_TOKEN not in channel.config ): # pragma: no cover return [], False - catalog_id = catalog.facebook_catalog_id - if not catalog_id: # pragma: no cover + if not facebook_catalog_id: # pragma: no cover return [], False start = timezone.now() @@ -175,7 +174,7 @@ def get_api_products(self, channel, catalog): # Retrieve the template domain, fallback to the default for channels # that have been setup earlier for backwards compatibility facebook_product_domain = "graph.facebook.com" - url = PRODUCT_LIST_URL % (facebook_product_domain, catalog_id) + url = PRODUCT_LIST_URL % (facebook_product_domain, facebook_catalog_id) product_data = [] while url: response = requests.get( diff --git a/temba/utils/whatsapp/tasks.py b/temba/utils/whatsapp/tasks.py index f6471c61622..28a05d88b7a 100644 --- a/temba/utils/whatsapp/tasks.py +++ b/temba/utils/whatsapp/tasks.py @@ -203,8 +203,20 @@ def update_channel_catalogs_status(channel, facebook_catalog_id, is_active): if is_active: Catalog.objects.filter(channel=channel, facebook_catalog_id=facebook_catalog_id).update(is_active=True) + sync_products_catalogs(channel, facebook_catalog_id) + return True +def sync_products_catalogs(channel, facebook_catalog_id): + try: + products_data, valid = channel.get_type().get_api_products(channel, facebook_catalog_id) + + catalog = Catalog.objects.filter(channel=channel, facebook_catalog_id=facebook_catalog_id) + update_local_products(catalog, products_data, channel) + + except Exception as e: + logger.error(f"Error refreshing WhatsApp catalog and products: {str(e)}", exc_info=True) + def set_false_is_active_catalog(channel, catalogs_data): channel.config["catalog_id"] = "" @@ -327,7 +339,7 @@ def refresh_whatsapp_catalog_and_products(): for channel in Channel.objects.filter(is_active=True, channel_type="WAC"): for catalog in Catalog.objects.filter(channel=channel, is_active=True): # Fetch products for each catalog - products_data, valid = channel.get_type().get_api_products(channel, catalog) + products_data, valid = channel.get_type().get_api_products(channel, catalog.facebook_catalog_id) if not valid: continue