OrderChatz 提供了三個 WordPress Action Hooks,讓第三方外掛可以在客戶備註的新增、編輯、刪除操作後執行自訂邏輯。
Hooks 列表
1. otz_saved_customer_note
當客戶備註成功儲存後觸發。
2. otz_updated_customer_note
當客戶備註成功更新後觸發。
3. otz_deleted_customer_note
當客戶備註成功刪除後觸發。
參數說明
所有 hooks 都接收單一陣列參數,包含以下欄位:
otz_saved_customer_note 參數
| 欄位 | 類型 | 說明 |
|---|---|---|
line_user_id | string | LINE 使用者 ID |
wp_user_id | int | LINE 用戶綁定的 WordPress 會員 ID (0 表示未綁定) |
note | string | 備註內容 |
category | string | 備註分類 |
related_product_id | int | 關聯的 WooCommerce 產品 ID (0 表示無關聯) |
related_message | array|null | 關聯的訊息資料,包含 datetime、content、type 欄位 |
otz_updated_customer_note 參數
| 欄位 | 類型 | 說明 |
|---|---|---|
line_user_id | string | LINE 使用者 ID |
wp_user_id | int | LINE 用戶綁定的 WordPress 會員 ID (0 表示未綁定) |
note | string | 更新後的備註內容 |
category | string | 更新後的備註分類 |
related_product_id | int | 更新後的關聯產品 ID (0 表示無關聯) |
old_data | object | 更新前的完整備註物件,包含所有欄位 |
otz_deleted_customer_note 參數
| 欄位 | 類型 | 說明 |
|---|---|---|
line_user_id | string | LINE 使用者 ID |
wp_user_id | int | LINE 用戶綁定的 WordPress 會員 ID (0 表示未綁定) |
deleted_data | object | 刪除前的完整備註物件,包含所有欄位 |
使用範例
基本用法
<?php
/**
* 備註儲存後發送通知
*/
add_action('otz_saved_customer_note', function($data) {
$line_user_id = $data['line_user_id'];
$wp_user_id = $data['wp_user_id'];
$note = $data['note'];
$category = $data['category'];
// 記錄日誌
error_log(sprintf(
'新增備註: LINE ID=%s, 分類=%s',
$line_user_id,
$category
));
// 如果用戶有綁定 WordPress 帳號
if ($wp_user_id > 0) {
$user = get_userdata($wp_user_id);
// 發送 Email 通知
wp_mail(
$user->user_email,
'您有新的客服備註',
sprintf('客服人員已為您添加備註: %s', $note)
);
}
});
備註更新後的處理
<?php
/**
* 比對備註變更並記錄
*/
add_action('otz_updated_customer_note', function($data) {
$old_note = $data['old_data']->note;
$new_note = $data['note'];
$old_category = $data['old_data']->category;
$new_category = $data['category'];
// 記錄變更歷史
$changes = array();
if ($old_note !== $new_note) {
$changes[] = sprintf('內容從「%s」改為「%s」', $old_note, $new_note);
}
if ($old_category !== $new_category) {
$changes[] = sprintf('分類從「%s」改為「%s」', $old_category, $new_category);
}
if (!empty($changes)) {
error_log('備註變更: ' . implode(', ', $changes));
}
});
備註刪除後的處理
<?php
/**
* 刪除備註時保留歷史記錄
*/
add_action('otz_deleted_customer_note', function($data) {
global $wpdb;
$deleted_note = $data['deleted_data'];
// 將刪除的備註存入歷史記錄表
$wpdb->insert(
$wpdb->prefix . 'custom_note_history',
array(
'line_user_id' => $data['line_user_id'],
'note_content' => $deleted_note->note,
'category' => $deleted_note->category,
'deleted_at' => current_time('mysql'),
),
array('%s', '%s', '%s', '%s')
);
});