在Woocommerce中的AdWords转换代码中添加订单数据 [英] Add Order data in Adword Conversion Code in Woocommerce

查看:110
本文介绍了在Woocommerce中的AdWords转换代码中添加订单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在子主题中添加的adwords转换代码。我想在此代码段的值属性中插入总购买金额,以便每次触发该代码时的总金额购物车已添加到转化中。

I have an adwords conversion code which I want to add in my child theme.I want to insert the Total purchase Amount in the "value" attribute in this piece of code so that each time the code is triggered the total amount in cart is added to the conversion.

<script async src="https://www.googletagmanager.com/gtag/js?id=AW-806400000"></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());

            gtag('config', 'AW-806400000"');
            gtag('event', 'conversion', {
                  'send_to': 'AW-806400000"/iHbjCOSfAewkasdowew',
                  'value': 1.0, **[Get the Total from cart and use here]**
                  'currency': 'USD',
                  'transaction_id': ''
              });

        </script>


推荐答案

更新

正如Reigel所建议的那样,应在已收到订单端点(谢谢页面)中更适当地使用它。在这里,我们改为定位订单总计(因为购物车对象已不存在)。

As Reigel suggested, it should be more appropriated in "Order received" end point (thankyou page). Here we target the Order total instead (as cart object doesn't exist anymore).

因此代码应为:

add_action('wp_head','google_tag_manager_checkout_conversion_script' );
function google_tag_manager_checkout_conversion_script() {
    // Only on "Order received" page
    if( ! is_wc_endpoint_url('order-received') ) 
        return; // Exit

    global $wp;

    $order_id  = absint( $wp->query_vars['order-received'] );
    $order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';

    if ( empty($order_id) || $order_id == 0 )
        return; // Exit

    $order = wc_get_order( $order_id );

    if ( $order->get_order_key() != $order_key )
        return; // Exit

    // Get Order total amount and Order transaction ID
    $order_total    = (float) $order->get_total();
    $transaction_id = $order->get_transaction_id();

    ?>
    <script async src="https://www.googletagmanager.com/gtag/js?id=AW-806400000"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'AW-806400000"');
        gtag('event', 'conversion', {
              'send_to': 'AW-806400000"/iHbjCOSfAewkasdowew',
              'value': <?php echo $order_total; ?>,
              'currency': 'USD',
              'transaction_id': '<?php echo $transaction_id; ?>'
          });
    </script>
    <?php
}

代码进入function.php文件您的活动子主题(或活动主题)。经过测试并有效。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这一次应该会更好,因为您这次获得了交易ID。

This should work better as you get the transaction ID this time.

原始答案为该AdWords脚本如何获得购物车总计的原始问题……

Original answer to the original question that how to get to cart total for this Adwords script…

显示您将使用的购物车总金额:

To display the total cart amount you will use:

<?php echo number_format( WC()->cart->total + WC()->cart->total_tax, 2 ); ?>

定位结帐页面,您可以尝试使用以下挂钩函数,该脚本会将脚本添加到< head> 部分具有正确的购物车总金额:

Targeting checkout page, you can try the following hooked function, that will add your script in the <head> section with the correct total cart amount:

add_action('wp_head','google_tag_manager_order_received_conversion_script' );
function google_tag_manager_order_received_conversion_script() {
    // Only on checkout page
    if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
    ?>
    <script async src="https://www.googletagmanager.com/gtag/js?id=AW-806400000"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'AW-806400000"');
        gtag('event', 'conversion', {
              'send_to': 'AW-806400000"/iHbjCOSfAewkasdowew',
              'value': <?php echo number_format( WC()->cart->total + WC()->cart->total_tax, 2 ); ?>,
              'currency': 'USD',
              'transaction_id': ''
          });
    </script>
    <?php
}

代码进入function.php文件您的活动子主题(或活动主题)。经过测试并可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.


但是似乎还很奇怪,因为尚未设置任何交易ID…

But it seems strange as there is not yet any transaction ID to set in it…

这篇关于在Woocommerce中的AdWords转换代码中添加订单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆