'product', 'value' => (float) $product->get_price(), 'currency' => get_woocommerce_currency(), 'contents' => [[ 'content_id' => (string) ($product->get_sku() ?: $product->get_id()), 'content_name' => $product->get_name(), 'content_category' => self::get_category_hierarchy($product), 'quantity' => 1, 'price' => (float) $product->get_price(), ]] ]; return $cache[$product_id] = $data; } /** * Formats data for the entire cart (InitiateCheckout) */ public static function get_cart_payload() { if (!WC()->cart) return null; $cart = WC()->cart; $data = [ 'content_type' => 'product', 'value' => (float) $cart->get_total('edit'), 'currency' => get_woocommerce_currency(), 'contents' => [] ]; foreach ($cart->get_cart() as $item) { $product = $item['data']; $data['contents'][] = [ 'content_id' => (string) ($product->get_sku() ?: ($product->get_id() ?: '')), 'content_name' => $product->get_name(), 'content_category' => self::get_category_hierarchy($product), 'quantity' => (int) $item['quantity'], 'price' => (float) $product->get_price(), ]; } return $data; } /** * Formats data for the final order (CompletePayment) */ public static function get_order_payload($order_id) { $order = wc_get_order($order_id); if (!$order) return null; $data = [ 'content_type' => 'product', 'value' => (float) $order->get_total(), 'currency' => $order->get_currency(), 'contents' => [] ]; foreach ($order->get_items() as $item_id => $item) { $product = $item->get_product(); $data['contents'][] = [ 'content_id' => (string) ($product->get_sku() ?: $product->get_id()), 'content_name' => $product->get_name(), 'quantity' => (int) $item->get_quantity(), 'price' => (float) $order->get_item_total($item, false, false), ]; } return $data; } /** * Helper to get the primary category name */ private static function get_category_hierarchy($product) { $terms = get_the_terms($product->get_id(), 'product_cat'); if ($terms && !is_wp_error($terms)) { return $terms[0]->name; } return 'Uncategorized'; } }