Blueprint
System Data Flow
The CRM-Aware AI Hook
AI / CRM Integration
A server-side WordPress function that hydrates an LLM system prompt with real purchase history and CRM segment data — making every AI-powered support response contextually aware of who the student is and what they've bought.
Logic Breakdown
When a support request is triggered, the hook resolves the current `user_id` and makes two parallel data calls: `wc_get_orders()` retrieves the last three WooCommerce transactions, and WP Fusion's adapter fetches the user's FluentCRM tags in a single round-trip. The combined context is JSON-encoded and injected into the LLM's system prompt, enabling personalised responses without the user having to re-explain their history.
Architecture Decisions
- 01WordPress hook intercepts an authenticated AJAX or REST request containing a valid `user_id`.
- 02`wc_get_orders()` retrieves the last 3 orders, mapped to their line items for purchase context.
- 03WP Fusion adapter fetches CRM tags/segments from FluentCRM in a single synchronous call.
- 04Context object is JSON-encoded and injected directly into the LLM system prompt payload.
Code Snippet
php// PHP / WordPress: Fetching User Context for AI Assistant
function get_student_context_for_ai($user_id) {
$orders = wc_get_orders(['customer' => $user_id, 'limit' => 3]);
$tags = wp_fusion()->user->get_tags($user_id); // Sync via WP Fusion
$context = [
"purchase_history" => array_map(fn($o) => $o->get_items(), $orders),
"certifications" => get_user_meta($user_id, 'completed_courses', true),
"crm_segment" => $tags
];
return json_encode($context);
}Key Dependencies
Known Limitations
- Synchronous PHP execution blocks the request thread — consider async via Action Scheduler for high traffic.
- Sensitive user data is in scope; always validate WP nonce and capability before calling.
Technical Spec
- Language
- PHP 8.1+
- Platform
- WordPress 6.x
- CRM
- FluentCRM + WP Fusion
- Commerce
- WooCommerce 8.x
- LLM
- Configurable (GPT-4 / Claude)
- Output
- JSON context string
- Hook
- wp_ajax_* / REST endpoint
Tags
Live Sandbox
Interactive runtime environment — The CRM-Aware AI Hook v1.2.0
$ npm run sandbox
> Initialising The CRM-Aware AI Hook v1.2.0…
> Status: Production
// Live iframe mounted once sandboxUrl is configured.