add_action( ‘woocommerce_before_checkout_form’, ‘send_facebook_initiate_checkout_event_on_checkout’ );
function send_facebook_initiate_checkout_event_on_checkout() {
global $woocommerce;
// Get the cart object
$cart = $woocommerce->cart;
// Get the cart total
$total = $cart->total;
// Get the currency code
$currency = get_woocommerce_currency();
// Get the products in the cart
$products = array();
foreach ( $cart->get_cart() as $item ) {
$product = wc_get_product( $item[‘product_id’] );
$products[] = array(
‘name’ => $product->get_name(),
‘id’ => $product->get_id(),
‘price’ => $product->get_price(),
‘quantity’ => $item[‘quantity’],
);
}
// Send the Facebook initiateCheckout event
?>
<script>
fbq(‘init’, ‘YOUR_FACEBOOK_PIXEL_ID’);
fbq(‘track’, ‘InitiateCheckout’, {
value: <?php echo $total; ?>,
currency: ‘<?php echo $currency; ?>’,
contents: <?php echo json_encode( $products ); ?>,
});
</script>
<?php
}
You will need to replace “YOUR_FACEBOOK_PIXEL_ID” with your actual Facebook Pixel ID.
if you already are sending the pixel id please remoce this line
fbq(‘init’, ‘YOUR_FACEBOOK_PIXEL_ID’);