全局站点标签不适用于 WordPress、WooCommerce

Global Site Tag not working with WordPress, WooCommerce

我很想知道下面的代码有什么问题。 我使用的是 WordPress + WooCommerce,当我上传此内容时,我的网站标签使我的网站崩溃 functions.php -> 全局站点标签在头部,由 google 标签 chrome 扩展检测 -> 转换跟踪代码在 functions.php 中,它只需要在订单完成时在感谢页面中可见。

这是我的 functions.php 页面。

欢迎任何帮助,我才刚刚开始 PHP。

朱利安



<?php

/**
 * Add custom tracking code to the thank-you page
 */
add_action( 'woocommerce_thankyou', 'conversion_tracking' );

function conversion_tracking( $order_id ) {

    // Lets grab the order
$order = new WC_Order( $order_id );

  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('event', 'conversion', {
        'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
        'value': '<?php echo $order->get_total(); ?>',
        'currency': '<?php echo $order->get_currency(); ?>',
        'transaction_id': '<?php echo $order_id; ?>'
    });
  </script>

    // This is the order total
    $order_total = $order->get_total();

    // This is how to grab line items from the order
    $line_items = $order->get_items();

    // This loops over line items
    foreach ( $line_items as $item ) {
        // This will be a product
        $product = $order->get_product_from_item( $item );

        // This is the products SKU
        $sku = $product->get_sku();

        // This is the qty purchased
        $qty = $item['qty'];

        // Line item total cost including taxes and rounded
        $total = $order->get_line_total( $item, true, true );

        // Line item subtotal (before discounts)
        $subtotal = $order->get_line_subtotal( $item, true, true );
    }
}

<?php
}


/*-----------------------------------------------------------------------------------*/
/*  Init theme framework
/*-----------------------------------------------------------------------------------*/
require( 'auxin/auxin-include/auxin.php' );
/*-----------------------------------------------------------------------------------*/

这是一些 woocommerce 和 google 示例页面 https://support.google.com/searchads/answer/9133542?hl=fr https://woocommerce.com/document/custom-tracking-code-for-the-thanks-page/#

这应该可以(有些功能部分您不需要 + 您需要回显脚本,因为您在 PHP 中):

add_action( 'woocommerce_thankyou', 'bloomer_conversion_tracking' );

function bloomer_conversion_tracking( $order_id ) {
   $order = wc_get_order( $order_id );
   ?>
   <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('event', 'conversion', {
        'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
        'value': '<?php echo $order->get_total(); ?>',
        'currency': '<?php echo $order->get_currency(); ?>',
        'transaction_id': '<?php echo $order_id; ?>'
    });
   </script>
   <?php
}

您的问题是您没有使用 echo 或任何方法来输出脚本。这导致了你的致命错误。

此外,您的脚本应添加 wc_enqueue_js 以干净地执行脚本。

function conversion_tracking( $order_id ) {

   // Lets grab the order
   $order = new WC_Order( $order_id );

   $script = "window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('event', 'conversion', {
        'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
        'value': '{$order->get_total()}',
        'currency': {$order->get_currency()}',
        'transaction_id': '{$order_id}'
    });";
    wc_enqueue_js($script);    
    
}

您的函数的其余部分可以清理,但这不是您问题的一部分。