Reputation: 83
I have been working on making the new facebook pixel tracking code function on my woocommerce site. I have a couple things I want to do with this, and I've started where I figured was the easiest place. Firstly, I want to change this code to use the checkout subtotal instead of the order total, because I use a deposit, which changes the order total to 20% of the actual price. Secondly, I use a pixel tracking plugin, and I'd like to add this function into the plugin to be able to quickly insert it into other pages. I've successfully been able to get the code to work by adding this to functions.php:
// Add purchase code to order received page
function checkout_analytics( $order_id ) {
$order = new WC_Order( $order_id );
$currency = $order->get_order_currency();
$total = $order->get_total();
$date = $order->order_date;
?>
<!-- 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', 'XXXXXXXXXX'); // Insert your pixel ID here.
</script>
<script>
fbq('track', 'Purchase', {
value: <?php echo $order->get_total(); ?>,
currency: 'USD'
});
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=XXXXXXXXXX&ev=PageView&noscript=1"
/></noscript>
<!-- DO NOT MODIFY -->
<!-- End Facebook Pixel Code -->
<?php
}
add_action( 'woocommerce_thankyou', 'checkout_analytics' );
So to try and get the order subtotal I've changed the code like this, which doesn't work. It doesn't pull the value of the subtotal, and I think I've got some formatting issues here. Pixel error picture
// Add purchase code to order received page
function checkout_analytics( $order_id ) {
$order = new WC_Order( $order_id );
$currency = $order->get_order_currency();
$total = $order->get_total();
$date = $order->order_date;
$cart_subtotal = $order->subtotal_ex_tax;
?>
<!-- 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', 'XXXXXXXXXXXXXX'); // Insert your pixel ID here.
</script>
<script>
fbq('track', 'Purchase', {
value: <?php echo $order->subtotal_ex_tax; ?>,
currency: 'USD'
});
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=XXXXXXXXX&ev=PageView&noscript=1"
/></noscript>
<!-- DO NOT MODIFY -->
<!-- End Facebook Pixel Code -->
<?php
}
add_action( 'woocommerce_thankyou', 'checkout_analytics' );
I'd like to get the code working properly, then I want to add this into a plugin, but I'm not sure how to do that entirely. My vague thought is to copy the function, but I feel as though that's not going to entirely work.
Upvotes: 1
Views: 2861
Reputation: 83
After continuing to work on this problem, I realized my error. If anyone is interested in using this for their facebook tracking pixel, all they need to do is modify the script to put their own tracking pixel id where you see XXXXXXXX
Here is the corrected code which functions correctly:
// Add purchase code to order received page
function checkout_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', 'XXXXXXXXX'); // Insert your pixel ID here.
</script>
<script>
fbq('track', 'Purchase', {
value: <?php echo $order->get_subtotal(); ?>,
currency: 'USD'
});
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=XXXXXXXXX&ev=PageView&noscript=1"
/></noscript>
<!-- DO NOT MODIFY -->
<!-- End Facebook Pixel Code -->
<?php
}
add_action( 'woocommerce_thankyou', 'checkout_analytics' );
Upvotes: 5