add_action('woocommerce_thankyou', 'marrybrown_openai_order_created', 20); function marrybrown_openai_order_created($order_id) { if (!$order_id || !function_exists('wc_get_order')) { return; } $order = wc_get_order($order_id); if (!$order) { return; } // Do not track failed, cancelled, refunded, or unpaid pending orders. if ($order->has_status(array('failed', 'cancelled', 'refunded', 'pending'))) { return; } // Real WooCommerce order total. $order_total = (float) $order->get_total(); // OpenAI requires monetary values in minor units. // Example: AED 87.50 = 8750. $amount_minor = (int) round($order_total * 100); // Real checkout currency, e.g. AED. $currency = strtoupper($order->get_currency()); $contents = array(); foreach ($order->get_items() as $item) { $product = $item->get_product(); if (!$product) { continue; } // Prefer SKU. Fall back to WooCommerce product ID. $product_id = $product->get_sku(); if (empty($product_id)) { $product_id = (string) $product->get_id(); } $contents[] = array( 'id' => (string) $product_id, 'name' => $item->get_name(), 'content_type' => 'product', 'quantity' => (int) $item->get_quantity(), ); } $event_data = array( 'type' => 'contents', 'amount' => $amount_minor, 'currency' => $currency, 'contents' => $contents, ); // Use the WooCommerce order ID as the unique OpenAI event ID. // Useful for deduplication if CAPI is added later. $event_options = array( 'event_id' => 'order_' . $order_id, ); ?>