跟踪添加购物车&为WooCommerce提交Facebook Pixel的订单 [英] Tracking Add too cart & submit Order for Facebook Pixel In WooCommerce

查看:133
本文介绍了跟踪添加购物车&为WooCommerce提交Facebook Pixel的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拼命地尝试了几天,以获取AddToCart的唯一跟踪和Facebook Pixel中的购买跟踪.默认设置为0.00会导致错误的转换结果.

I am desperately trying for days to get unique tracking for AddToCart and Purchase Tracking in Facebook Pixel. Having a default setting of 0.00 gives wrong conversion results.

我拼命地尝试了几天,以获取AddToCart的唯一跟踪和Facebook Pixel中的购买跟踪.默认设置为0.00会导致错误的转换结果.

I am desperately trying for days to get unique tracking for AddToCart and Purchase Tracking in Facebook Pixel. Having a default setting of 0.00 gives wrong conversion results.

我们正在使用WooCommerce,我正在通过标签管理器实施FB Pixel.

We are using WooCommerce and I am implementing FB Pixel Through Tag Manager.

所有事件都在跟踪中,但是我想获取AddToCart的AddToCart值以及购买时的购物车总额. WooCommerce使用的是数据层,我尝试使用ecomm_total和price的数据层,但出现错误

All events are tracking but I want to get a AddToCart value for AddToCart and a total Cart amount on purchasing. WooCommerce is using a data layer and I tried to use the Data Layer of ecomm_total and price but I get the error

无效的AddToCart值参数

Invalid AddToCart Value Parameter

下面是我们的代码示例

是否有可以替换0.00的值以获得每种产品和订单的正确值?

Is there any value I can replace 0.00 with to get the correct values for each product and order?

<script>
fbq('track', 'AddToCart', {
value: 0.00,
currency: 'SEK',
});
</script>

推荐答案

基于

Based on Conditional Logic and custom FB Pixels Integration into WooCommerce answer code, the following will set main events and price values from "AddToCart" and "Purchase" events:

// Function that gets the Ajax data
add_action( 'wp_ajax_product_added_to_cart', 'wc_product_added_to_cart' );
add_action( 'wp_ajax_nopriv_product_added_to_cart', 'wc_product_added_to_cart' );
function wc_product_added_to_cart() {
    if ( isset($_POST['pid']) ){
        // Get an instance of the WCW_Product Object
        $product = wc_get_product( $_POST['pid'] );

        // Return the product price including taxes
        echo number_format( wc_get_price_including_tax( $product ), 2 );

        // OR =>the product price EXCLUDING taxes
        // echo number_format( wc_get_price_excluding_tax( $product ), 2 );
    }
    die(); // Alway at the end (to avoid server error 500)
}

// FB PiXELS HEAD Script (Initializing)
add_action( 'wp_head', 'fbpixels_head_script' );
function fbpixels_head_script() {
    ?>
    <script>
    !function(f,b,e,v,n,t,s)
    {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
    n.callMethod.apply(n,arguments):n.queue.push(arguments)};
    if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
    n.queue=[];t=b.createElement(e);t.async=!0;
    t.src=v;s=b.getElementsByTagName(e)[0];
    s.parentNode.insertBefore(t,s)}(window, document,'script',
    'https://connect.facebook.net/en_US/fbevents.js');
    </script>
    <?php
}

// FB PiXELS Footer script Events
add_action( 'wp_footer', 'fbpixels_add_to_cart_script' );
function fbpixels_add_to_cart_script(){
    // HERE set your init reference
    $init_id = '1552143158136112';

    // HERE set the product currency code
    $currency = 'SEK';

    ## 0. Common script -  On ALL Pages
    ?>
    <script>
    fbq('init', <?php echo $init_id; ?>);
    fbq('track', 'PageView');
    <?php

    ## 1. On Checkout page
    if ( ! is_wc_endpoint_url( 'order-received' ) && is_checkout() ) {
    ?>
    fbq('track', 'InitiateCheckout');
    <?php

    ## 2. On Order received (thankyou)
    } elseif ( is_wc_endpoint_url( 'order-received' ) ) {
    global $wp;

    // Get the Order ID from Query vars
    $order_id  = absint( $wp->query_vars['order-received'] );

    if ( $order_id > 0 ){

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );
    ?>
    fbq('track', 'Purchase', {
        value:    <?php echo $order->get_total(); ?>,
        currency: '<?php echo $order->get_order_currency(); ?>',
    });
    <?php
    }
    ## 3. Other pages - (EXCEPT Order received and Checkout)
    } else {

    ?>
    jQuery(function($){
        if (typeof woocommerce_params === 'undefined')
            return false;

        var price;
        $('body').on( 'adding_to_cart', function(a,b,d){
            var sku = d.product_sku, // product Sku
                pid = d.product_id,  // product ID
                qty = d.quantity;    // Quantity

            $.ajax({
                url: woocommerce_params.ajax_url,
                type: 'POST',
                data: {
                    'action' : 'product_added_to_cart',
                    'sku'    : sku,
                    'pid'    : pid,
                    'qty'    : qty
                },
                success: function(result) {
                    // The FB Pixels script for AddToCart
                    if( result > 0 ){
                        fbq('track', 'AddToCart', {
                            value:    result,
                            currency: '<?php echo $currency; ?>',
                        });
                        console.log(result);
                    }
                }
            });
        });
    });
    <?php
    }
    ## For single product pages - Normal add to cart
    if( is_product() && isset($_POST['add-to-cart']) ){
        global $product;

        // variable product
        if( $product->is_type('variable') && isset($_POST['variation_id']) ) {
            $product_id = $_POST['variation_id'];
            $price = number_format( wc_get_price_including_tax( wc_get_product($_POST['variation_id']) ), 2 );
        }
        // Simple product
        elseif ( $product->is_type('simple') ) {
            $product_id = $product->get_id();
            $price = number_format( wc_get_price_including_tax( $product ), 2 );
        }
        $quantity = isset($_POST['quantity']) ? $_POST['quantity'] : 1;

        ?>
        fbq('track', 'AddToCart', {
            value:    <?php echo $price; ?>,
            currency: '<?php echo $currency; ?>',
        });
        <?php
    }
    ?>
    </script>
    <noscript>
    <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=<?php echo $init_id; ?>&ev=PageView&noscript=1" />
    </noscript>
    <?php
}

代码进入您的活动子主题(或活动主题)的function.php文件中.似乎可行.

Code goes in function.php file of your active child theme (or active theme). It seems working.

这篇关于跟踪添加购物车&amp;为WooCommerce提交Facebook Pixel的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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