How to Build a Chat-Based WooCommerce Admin Plugin Without Using External APIs
Imagine being able to query your WooCommerce store like this:
“What are my top selling products this month?”
Usually, this kind of functionality requires using AI services like ChatGPT. But what if you want to do it entirely offline, with no external API, and complete control over your data?
In this tutorial, you’ll learn how to create a WooCommerce admin plugin that allows chat-style queries — like a simplified ChatGPT — using local PHP and SQL logic only.
ChatGPT vs Rule-Based Plugin: Key Differences
Feature | ChatGPT | Rule-Based Plugin |
---|---|---|
Language Understanding | AI understands intent, synonyms, and structure | Needs exact keywords or regex |
Flexibility | Can handle many variations naturally | Every variation must be manually defined |
SQL Generation | Dynamically generated from prompt | Static queries |
External Dependency | Yes (OpenAI or other LLM API) | No |
Privacy | Data sent to external server | All local |
Step-by-Step: Create the Plugin
Step 1: Create the Plugin File
Create a folder wc-admin-chat
in wp-content/plugins/
, and inside it, create a file called wc-admin-chat.php
with the following code:
<?php
/*
Plugin Name: WooCommerce Admin Chat
Description: Chat-like interface to query WooCommerce data.
Version: 1.0
Author: Kishore
*/
add_action('admin_menu', 'wc_admin_chat_menu');
function wc_admin_chat_menu() {
add_menu_page('WC Chat', 'WC Chat', 'manage_woocommerce', 'wc-admin-chat', 'wc_admin_chat_page');
}
function wc_admin_chat_page() {
?>
<div class="wrap">
<h1>WooCommerce Chat Assistant</h1>
<div id="chat-box" style="max-width:600px;">
<div id="chat-log" style="background:#f9f9f9;padding:15px;height:300px;overflow-y:auto;border:1px solid #ccc;"></div>
<form id="wc-chat-form" method="post">
<input type="text" name="wc_chat_prompt" id="wc_chat_prompt" style="width:80%;">
<button type="submit" class="button button-primary">Ask</button>
</form>
</div>
</div>
<script>
const form = document.getElementById('wc-chat-form');
const log = document.getElementById('chat-log');
form.addEventListener('submit', async function(e) {
e.preventDefault();
const input = document.getElementById('wc_chat_prompt');
const userMsg = input.value;
log.innerHTML += `<p><strong>You:</strong> ${userMsg}</p>`;
input.value = '';
const res = await fetch(ajaxurl, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'action=wc_process_admin_chat&prompt=' + encodeURIComponent(userMsg)
});
const data = await res.text();
log.innerHTML += `<p><strong>Bot:</strong> ${data}</p>`;
log.scrollTop = log.scrollHeight;
});
</script>
<?php
}
Step 2: Add AJAX Logic to Handle Queries
Below the above code, add this:
add_action('wp_ajax_wc_process_admin_chat', 'wc_process_admin_chat');
function wc_process_admin_chat() {
if (!current_user_can('manage_woocommerce')) wp_die();
$prompt = strtolower(sanitize_text_field($_POST['prompt']));
global $wpdb;
if (strpos($prompt, 'top') !== false && strpos($prompt, 'selling') !== false) {
$results = $wpdb->get_results("
SELECT p.post_title, SUM(oim.meta_value) as total_qty
FROM {$wpdb->prefix}woocommerce_order_items oi
JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim ON oi.order_item_id = oim.order_item_id
JOIN {$wpdb->prefix}posts p ON oim.meta_value = p.ID
WHERE oim.meta_key = '_product_id'
GROUP BY p.ID
ORDER BY total_qty DESC
LIMIT 5
");
foreach ($results as $row) {
echo esc_html($row->post_title) . " - Sold: " . intval($row->total_qty) . "<br>";
}
wp_die();
}
echo "Sorry, I didn’t understand that.";
wp_die();
}
Making It Smarter with Intent Matching
You can build a lightweight NLP layer using similar_text()
or levenshtein()
to loosely match prompts:
$intent_map = [
'top selling products' => 'get_top_products',
'orders this month' => 'get_orders_this_month',
'top customers' => 'get_top_customers',
];
foreach ($intent_map as $key => $func) {
similar_text($prompt, $key, $percent);
if ($percent > 75) {
call_user_func($func);
exit;
}
}
Then define those functions to run the right SQL query. It’s not as powerful as ChatGPT, but much more flexible than hardcoding only one phrase.
Bonus: Add More Commands
- “Orders this month”: Count orders from
post_date
- “Low stock items”: Query
_stock
meta - “Top customers”: Join
orders
andusers
to sum totals
These are all SQL queries you can map to phrases using basic logic.
Want ChatGPT-Like Behavior?
If you’re ready to take it further, explore:
- Ollama or Local LLMs: Run models like Mistral, LLaMA on your server
- LangChain: Translate prompts to SQL dynamically
- PrivateGPT: For RAG-based querying of your own data
This setup would require a backend bridge between PHP (WordPress) and Python (LLM API), but would give you near-GPT power, locally.
Conclusion
Building a WooCommerce chat assistant without external APIs is entirely possible using PHP, SQL, and clever prompt handling. While it’s not as intelligent as ChatGPT, it’s fast, private, and completely under your control.
Want more prompts and SQL mappings to extend it further? Just ask in the comments!
Leave a Reply