0

add_filter('wpm_send_http_api_facebook_capi_requests_blocking', '__return_true');
add_action('wp_footer', function() {
    if (is_page('thank-you-order') && isset($_GET['order_id'])) {
        $order_id = intval($_GET['order_id']); // Sanitize the input
        $order = wc_get_order($order_id);
         if (!$order) return;
         $order_total = $order->get_total();
        $currency = get_woocommerce_currency();
        $order_items = [];
         foreach ($order->get_items() as $item) {
            $product = $item->get_product();
            $order_items[] = [
                'id' => $product->get_id(),
                'name' => $product->get_name(),
                'quantity' => $item->get_quantity(),
                'price' => $product->get_price()
            ];
        }
         $order_items_json = json_encode($order_items);
        $order_id_string = strval($order_id); // Ensure order ID is passed as a string
        ?>
         <script>
            fbq('track', 'Purchase', {
                value: <?php echo json_encode($order_total); ?>,
                currency: "<?php echo esc_js($currency); ?>",
                order_id: "<?php echo esc_js($order_id_string); ?>",
                page_url: "<?php echo esc_url(home_url($_SERVER['REQUEST_URI'])); ?>",
                contents: <?php echo $order_items_json; ?>,
                content_type: "product"
            });
        </script>
         <?php
    }
});

Jagdish Sarma Asked question March 19, 2025
Add a Comment