将 Facebook 像素购买事件代码添加到 WooCommerce 购买完成页面

Add Facebook Pixel Purchase Event Code to WooCommerce purchase completion page

我需要在 WooCommerce 的购买完成页面中添加以下内容:

Copy the event code snippet. You can add parameters to send additional on-page data. fbq('track', 'Purchase');

我尝试将以下代码添加到子主题 functions.php 文件中:

add_action('wp_enqueue_scripts', 'qg_enqueue');
function qg_enqueue() {
    if (is_order_received_page()) {
        wp_enqueue_script(
            fbq('track', 'Purchase');
        );
    }
}

致命错误。我确定我搞砸了,但我有点迷路了。我尝试了很多搜索。我正在尝试仅将脚本添加到订单接收页面 WooCommerce Checkout 端点。怎么了?

您的代码在 wp_enqueue_script() 函数中缺少引号,因此尝试将 fbq('track', 'Purchase'); 替换为 "fbq('track', 'Purchase');",应该可以解决错误。

现在你最好使用 wc_enqueue_js() functiontemplate_redirect 钩子如下:

add_action('template_redirect', 'enqueue_fbq_purchase_event');
function enqueue_fbq_purchase_event() {
    if ( is_order_received_page() ) {
        wc_enqueue_js( "fbq('track', 'Purchase');" );
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。它应该更好用。