条件逻辑和自定义FB像素集成到WooCommerce [英] Conditional Logic and custom FB Pixels Integration into WooCommerce

查看:159
本文介绍了条件逻辑和自定义FB像素集成到WooCommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求:

1)在WooCommerce结帐页面上触发FB像素-即相关事件. (即添加到购物车/发起结帐)

1) Fire FB Pixel on WooCommerce checkout page - associated events that is. (ie Add To Cart/Initiate Checkout)

2)WooCommerce的结帐页面/感谢页面上的火灾购买事件.

2) Fire Purchase event on check-out page/thank-you page of WooCommerce.

3)在不属于上述两个网站的整个网站上,保持FB Pixel正常.

3) Keep FB Pixel as normal across entire sites that are not part of the above two.

有人告诉我条件逻辑是解决之道.但是,我很困惑如何将下面的钩子(在第二篇文章中)合并到我的"elseif"语句中.

I was told conditional logic is the way to go. Yet, I am stumped at how to incorporate the hook below (in my 2nd post) into my "elseif" statement.

脚本1

add_action( 'wp_head', 'facebook_tracking' );

function facebook_tracking() {

if ( is_checkout() && is_wc_endpoint_url( 'order-received' ) ) {

// TARGET THE CHECKOUT PAGE WITH is_checkout()
// THEN EXCLUDE THE THANK YOU PAGE USING CONDITIONAL LOGIC

    ?>
        <!-- Facebook Pixel Code for Initiated Checkout -->
     <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');
  fbq('init', 'XXX');
  fbq('track', 'PageView');
  fbq('track', 'InitiateCheckout');
  fbq('track', 'AddToCart');
</script>
<noscript><img height="1" width="1" style="display:none"
  src="https://www.facebook.com/tr?id=[]&ev=PageView&noscript=1"
/></noscript>
        <!-- End Facebook Pixel Code -->
  // NOW MY THANK YOU PAGE - I WANT TO EMBED AN ADDITIONAL ADD_ACTION BUT HOW? - SEE MY NEXT MESSAGE
    <?php } elseif ( is_wc_endpoint_url( 'order-received' ) ) { ?>
  <!-- Facebook Pixel Code for Conversions -->
<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');
  fbq('init', 'XXX');
  fbq('track', 'PageView');
  fbq('track', 'Purchase', {
    value: <?php echo $order->get_total(); ?>,
    currency: 'AUD',
  });

</script>
<noscript><img height="1" width="1" style="display:none"
  src="https://www.facebook.com/tr?id=1552143158136128&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
 <?php } else { // FINALLY WE TARGET ALL THE OTHER PAGES ?>
<!-- Facebook Pixel Code for Rest of Website -->
<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');
  fbq('init', 'XXX');
  fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
  src="https://www.facebook.com/tr?id=[]&ev=PageView&noscript=1"
/></noscript>
        <!-- End Facebook Pixel Code -->
    <?php
} 

}

脚本#2

如何在我的elseif语句中嵌入以下钩子:

How do I embed the following hook into my elseif statement:

    function purchase_analytics( $order_id ) {
    $order = new WC_Order( $order_id );
    $currency = $order->get_order_currency();
    $total = $order->get_total();
    $date = $order->order_date;
    $checkout_subtotal = $order->get_subtotal();
    ?>
<!-- Facebook Pixel Code -->
<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');
fbq('init', 'XXX');
</script>
<script>
fbq('track', 'Purchase', {
value: <?php echo $order->get_total(); ?>,
currency: 'AUD'
});
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=[]&ev=PageView&noscript=1"
/></noscript>
<!-- DO NOT MODIFY -->
<!-- End Facebook Pixel Code -->
    <?php   
}
add_action( 'woocommerce_thankyou', 'purchase_analytics' );

任何帮助将不胜感激!

Loic,请参见以下内容:

Loic please see below:

add_action( 'wp_head', 'facebook_tracking' );
function facebook_tracking() {

    // 1. Common script -  On ALL Pages --> works fine
    $init = 'XXX';
    ?>
     <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');

    fbq('init', <?php echo $init; ?>);
    fbq('track', 'PageView');
    <?php

    // 2. On Checkout --> works fine, how do I call for values of the below, particularly InitiateCheckout?
    if( ! is_wc_endpoint_url( 'order-received' ) && is_checkout() ) :
    ?>
    fbq('track', 'InitiateCheckout');
  value:
  currency: ''
    fbq('track', 'AddToCart');
    <?php

    // 3. On Order received --> not currently firing.
    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 );
      // is this ok? foreach( $order->get_items() as $item_id => $item ){
        // is this ok? $product = $item->get_product();
        // is this ok? $product_id = $item->get_product_id();
    $currency = $order->get_order_currency();
    $total = $order->get_total();
    $date = $order->order_date;
      }
    ?>
    fbq('track', 'Purchase', {
        value:    <?php echo $order->get_total(); ?>,
        currency: <?php echo $order->get_order_currency(); ?>,
      content_ids: <?php echo $item->get_product_id(); ?>,
      // content_type: ???
    });
    <?php
      }

    // 4. Other pages (EXCEPT order received and checkout) --> did not work originally but want to fire this on my upsell page (1 specific URL). Custom Event based.
    else ; //was needing ; not :
    ?>
    fbq('track', 'CUSTOMEVENT');
    <?php
    endif;
    ?>
    </script>
    <noscript><img height="1" width="1" style="display:none"
  src="https://www.facebook.com/tr?id=[XXX]&ev=PageView&noscript=1"
/></noscript>
<?php
  }
}

感谢您的帮助.

推荐答案

已更新-在钩在wp_head钩中的钩子函数中,您的两个条件错误地定位到收到订单",所以:

Updated - In your hooked function hooked in wp_head hook, two of your conditions are targeting "Order received" by mistake, so:

if ( is_checkout() && is_wc_endpoint_url( 'order-received' ) ) {

是同一件事,只是:

if ( is_wc_endpoint_url( 'order-received' ) ) {


有些重复(可以在任何地方使用的通用脚本)…


There is some repetitive (common script that you can use everywhere)…

似乎还有其他错误……请尝试以下重新访问的代码:

It seems that there is some other mistakes… Try the following revisited code:

add_action( 'wp_head', 'facebook_tracking' );
function facebook_tracking() {

    // 1. Common script -  On ALL Pages
    $init = 'XXX';
    ?>
     <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');

    fbq('init', <?php echo $init; ?>);
    fbq('track', 'PageView');
    <?php

    // 2. On Checkout
    if( ! is_wc_endpoint_url( 'order-received' ) && is_checkout() ) : 
    ?>
    fbq('track', 'InitiateCheckout');
    <?php 

    // 3. On Order received 
    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 
    } 

    // 4. Other pages (EXCEPT order received and checkout)
    else : 
    ?>
    fbq('track', 'AddToCart');
    <?php 
    endif;
    ?>
    </script>
    <noscript><img height="1" width="1" style="display:none"
  src="https://www.facebook.com/tr?id=1552143158136128&ev=PageView&noscript=1"
/></noscript>
    <?php
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.应该可以.

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

这篇关于条件逻辑和自定义FB像素集成到WooCommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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