Home / eCommerce / Meta Pixel – Purchase Event
Duplicate Snippet

Embed Snippet on Your Site

Meta Pixel – Purchase Event

Meta Pixel - Purchase Event

Code Preview
php
<?php
/**
 * =============================================================================
 * META PIXEL — Purchase (WooCommerce Thank You) — Dedup ready for CAPI
 * =============================================================================
 * - Fires on thank-you page ONLY for paid orders (processing/completed)
 * - Uses variation_id when present
 * - Adds eventID = order_{order_id} for CAPI dedup
 * - Prevents repeats via order meta guard
 * =============================================================================
 */
add_action('woocommerce_thankyou', 'voelgoed_meta_pixel_purchase', 10, 1);
function voelgoed_meta_pixel_purchase($order_id) {
    if (empty($order_id) || is_admin() || wp_doing_ajax()) {
        return;
    }
    if ( ! function_exists('wc_get_order') ) {
        return;
    }
    $order = wc_get_order($order_id);
    if (!$order) {
        return;
    }
    // Only fire for PAID orders (prevents false purchases)
    $status = $order->get_status();
    if ( ! in_array($status, ['processing', 'completed'], true) ) {
        return;
    }
    // Prevent duplicates (page refresh / re-visits)
    if (get_post_meta($order_id, '_vg_meta_pixel_purchase_tracked', true)) {
        return;
    }
    $content_ids = [];
    $contents    = [];
    foreach ($order->get_items() as $item) {
        if (!is_a($item, 'WC_Order_Item_Product')) {
            continue;
        }
        $product_id   = (int) $item->get_product_id();
        $variation_id = (int) $item->get_variation_id();
        $id           = $variation_id ? $variation_id : $product_id;
        $qty = (int) $item->get_quantity();
        if ($id <= 0 || $qty <= 0) {
            continue;
        }
        // Unit price (ex tax) from line total / qty
        $line_total = (float) $item->get_total();
        $unit_price = $qty > 0 ? ($line_total / $qty) : 0.0;
        $content_ids[] = (string) $id;
        $contents[] = [
            'id'         => (string) $id,
            'quantity'   => $qty,
            'item_price' => round((float) $unit_price, 2),
        ];
    }
    $content_ids = array_values(array_unique($content_ids));
    $value    = (float) $order->get_total();
    $currency = $order->get_currency() ? $order->get_currency() : 'ZAR';
    // eventID must match server-side CAPI event_id for dedup
    $event_id = 'order_' . (string) $order_id;
    ?>
    <script>
    (function () {
        if (typeof fbq !== 'function') return;
        fbq('track', 'Purchase', {
            content_ids: <?php echo wp_json_encode($content_ids); ?>,
            content_type: 'product',
            contents: <?php echo wp_json_encode($contents); ?>,
            value: <?php echo wp_json_encode($value); ?>,
            currency: <?php echo wp_json_encode($currency); ?>
        }, {
            eventID: <?php echo wp_json_encode($event_id); ?>
        });
    })();
    </script>
    <?php
    // Mark tracked AFTER output (best possible server-side guard)
    update_post_meta($order_id, '_vg_meta_pixel_purchase_tracked', 1);
}

Comments

Add a Comment