Automatically Tag WooCommerce Customer Notes with AI-Like Fuzzy Matching
When running a WooCommerce store, you may have noticed that customers often leave notes like:
“Please send this fast, it’s a birthday gift. Thanks!”
These notes are helpful — but manually reading and reacting to them for each order can be tedious. That’s where auto-tagging comes in. In this post, you’ll learn how to automatically detect and tag common intent keywords in customer notes using PHP and fuzzy matching (Levenshtein distance), and store those tags in the order meta.
🧠 Why Auto-Tag Customer Notes?
By tagging notes like:
- “Urgent”
- “Birthday”
- “Gift”
- “Thank You”
- “Delayed”
- “International”
You can:
- Show different icons or alerts for special cases.
- Sort/filter orders by tags.
- Automate fulfillment logic (e.g., prioritize urgent orders).
- Analyze customer sentiment or intent trends.
🛠️ How It Works
We’ll hook into WooCommerce when an order is created or updated. Then:
- Grab the customer note.
- Run it through a matching engine that checks for keywords using:
- Direct matches
- Fuzzy matching (using
levenshtein()
for typos like “brithday”) - Contextual rules (e.g., don’t tag as Thank You unless the message is short and appreciative)
- Store the tags in custom order meta.
📦 Full Code (WooCommerce Plugin)
You can drop this code into a custom plugin or your theme’s functions.php
.
Here’s the complete version for download
<?php
/**
* Plugin Name: WooCommerce Auto Note Tagger
* Description: Auto-tags order notes using fuzzy matching and stores tags in order meta.
* Version: 1.0
* Author: Kishore
*/
if (!defined('ABSPATH')) exit;
add_action('woocommerce_new_order', 'wc_autotagger_process_order', 20, 2);
add_action('woocommerce_process_shop_order_meta', 'wc_autotagger_process_order_from_post', 20, 2);
function wc_autotagger_process_order( $order_id, $order ) {
if ( ! $order || ! is_a( $order, 'WC_Order' ) ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
}
$note = $order->get_customer_note();
if ( empty( $note ) ) {
return;
}
$tags = wc_autotagger_detect_tags( $note );
if ( ! empty( $tags ) ) {
$order->update_meta_data( 'customer_note_tags', implode( ',', $tags ) );
$order->save();
}
}
function wc_autotagger_detect_tags($note) {
$tag_rules = [
'Urgent' => [
'keywords' => ['urgent', 'urgnt', 'rush', 'asap', 'immediate', 'now', 'fast'],
'fuzzy' => true
],
'Gift' => [
'keywords' => ['gift', 'gfit', 'gifting', 'gifted', 'giftwrap', 'present'],
'fuzzy' => true
],
'Birthday' => [
'keywords' => ['birthday', 'bday', 'birthdy', 'brithday', 'anniversary', 'b-day'],
'fuzzy' => true
],
'International' => [
'keywords' => ['intl', 'international', 'abroad', 'overseas'],
'fuzzy' => false
],
'Delayed' => [
'keywords' => ['delay', 'delayed', 'late', 'postpone', 'reschedule'],
'fuzzy' => true
],
'Thank You' => [
'keywords' => ['thankyou', 'thanks', 'grateful', 'appreciate'],
'fuzzy' => false,
'only_if_appreciation' => true
],
];
$threshold = 2;
$min_score = 1;
$scores = [];
$original_note = $note;
$note = strtolower(remove_accents($note));
$note = preg_replace('/[^\w\s]/', '', $note);
$words = preg_split('/\s+/', $note);
foreach ($tag_rules as $tag => $rule) {
$score = 0;
foreach ($rule['keywords'] as $keyword) {
foreach ($words as $word) {
if (strlen($word) < 3) continue;
if ($word === $keyword || strpos($word, $keyword) !== false) {
$score++;
break;
}
if (!empty($rule['fuzzy']) && levenshtein($word, $keyword) <= $threshold) {
$score++;
break;
}
}
}
// Special handling for Thank You
if (!empty($rule['only_if_appreciation'])) {
if ($score > 0 && strlen($original_note) < 100 && preg_match('/^(thank|thanks|grateful|appreciate)/i', trim($original_note))) {
$scores[$tag] = $score;
}
} elseif ($score >= $min_score) {
$scores[$tag] = $score;
}
}
return array_keys($scores);
}
function wc_autotagger_process_order_from_post($order_id, $post) {
$order = wc_get_order($order_id);
if ($order) {
wc_autotagger_process_order($order_id, $order);
}
}
🧪 Example
Given this note:
“Please send this fast, it’s a birthday gift. Thanks!”
Your plugin will auto-tag:
Urgent, Gift, Birthday, Thank You
Which is stored in the order’s custom meta as customer_note_tags
.
Final Thoughts
This small feature brings automation and clarity to your order processing workflow. It’s simple, powerful, and works out of the box — all without needing AI tools or extra SaaS costs.
Leave a Reply