简体   繁体   English

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

[英]Add Facebook Pixel Purchase Event Code to WooCommerce purchase completion page

I need to add the following to the purchase completion page in WooCommerce:我需要在WooCommerce的购买完成页面中添加以下内容:

Copy the event code snippet.复制事件代码片段。 You can add parameters to send additional on-page data.您可以添加参数以发送额外的页面数据。 fbq('track', 'Purchase'); fbq('追踪', '购买');

I tried adding the following code to the child theme functions.php file:我尝试将以下代码添加到子主题 functions.php 文件中:

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

Fatal error.致命错误。 I'm sure I'm messing something up but I'm a little lost.我确定我搞砸了,但我有点迷路了。 I tried quite a bit of searching.我尝试了相当多的搜索。 I'm trying to add the script only to the order received page, WooCommerce Checkout endpoint.我正在尝试仅将脚本添加到收到的订单页面 WooCommerce 结帐端点。 What's wrong?怎么了?

There are missing quotes in your code inside wp_enqueue_script() function, so try to replace fbq('track', 'Purchase'); wp_enqueue_script() function 中的代码中缺少引号,因此请尝试替换fbq('track', 'Purchase'); with "fbq('track', 'Purchase');""fbq('track', 'Purchase');" , it should solve the error. ,它应该解决错误。

Now you should better use wc_enqueue_js() function using template_redirect hook as follows:现在你应该更好地使用wc_enqueue_js() function使用template_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');" );
    }
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 It should better work.它应该更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM