Advanced Dynamic Facebook Purchase Event – Woocommerce

add_action( 'woocommerce_thankyou', 'fb_custom_params_thank_you_page' );

function fb_custom_params_thank_you_page($order_id){
$order = wc_get_order( $order_id );
$order_data = $order->get_data();
$order_total = $order_data['cart_tax'];

?>
<script>
setTimeout(function(){
fbq('track', 'Purchase',
{
value: <?php echo $order_total ?>,
currency: 'AUD',
contents: [
<?php
foreach ($order->get_items() as $item_key => $item ):

$item_id = $item->get_id();

$product_id = $item->get_product_id();
$quantity = $item->get_quantity();

?>
{
id: '<?php echo $product_id; ?>',
quantity: <?php echo $quantity; ?>
},
<?php
$int++;
endforeach;
?>
],
content_type: 'product'
}
);
},600);
</script>
<!-- <?php var_dump($order_data); ?> -->
<?php
}

 

Place the above code into your functions.php file inside of your WordPress theme. This will iterate through products in the completed order and serve the correct id, value, content_type and quantity for each product.

Want a Fast Server Like Mine?

GET VULTR!