概要
woocommerce_order_note_added
は、WooCommerceにおいて注文ノートが追加された際に発火するフックです。このアクションは、特定の機能を実装することができ、以下のような用途でよく使われます。
- 注文ノートが追加されたタイミングで通知を送信する。
- 注文ノートの内容をログとして保存する。
- 注文ノートが追加された際にカスタムアクションをトリガーする。
- ユーザーに対するフィードバックメッセージを表示する。
- 注文ノートに関するデータを別のデータベースに保存する。
- 特定の条件に応じて、ノートの内容を修正する。
構文
add_action('woocommerce_order_note_added', 'your_custom_function', 10, 2);
パラメータ
$note_id
(int) – 追加されたノートのID。$order_id
(int) – 注文のID。
戻り値
このアクションは戻り値を持ちません。
使用可能なバージョン
- WooCommerce: 3.0.0以降
- WordPress: 4.0以降
この関数のアクションでの使用可能性
アクション | 使用例 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
サンプルコード
サンプル1: 注文ノート追加時のメール通知
このコードは、新しい注文ノートが追加された際に特定のユーザーにメール通知を送信します。
add_action('woocommerce_order_note_added', 'send_order_note_email', 10, 2);
function send_order_note_email($note_id, $order_id) {
$order = wc_get_order($order_id);
$note = get_comment($note_id);
// メール送信処理
wp_mail('example@example.com', '新しい注文ノート', $note->comment_content);
}
引用元: https://www.example.com
サンプル2: 注文ノートの内容をログに記録
このコードは、新しい注文ノートの内容をカスタムログファイルに記録します。
add_action('woocommerce_order_note_added', 'log_order_note', 10, 2);
function log_order_note($note_id, $order_id) {
$note = get_comment($note_id);
error_log("注文ID: $order_id のノート: " . $note->comment_content);
}
引用元: https://www.example.com
サンプル3: 特定の条件でノート内容を修正
このコードでは、特定の文字列が含まれる場合にノート内容を修正します。
add_action('woocommerce_order_note_added', 'modify_order_note', 10, 2);
function modify_order_note($note_id, $order_id) {
$note = get_comment($note_id);
if (strpos($note->comment_content, '特定のキーワード') !== false) {
// ノート内容の変更
wp_update_comment(array(
'comment_ID' => $note_id,
'comment_content' => '修正された内容'
));
}
}
引用元: https://www.example.com
サンプル4: 注文ノート追加時のカスタムアクション
このコードでは、注文ノートが追加された際にカスタムアクションをトリガーします。
add_action('woocommerce_order_note_added', 'trigger_custom_action', 10, 2);
function trigger_custom_action($note_id, $order_id) {
do_action('custom_order_note_added', $note_id, $order_id);
}
引用元: https://www.example.com
サンプル5: 注文ノート追加時のデータ保存
このコードは、新しいノートが追加された際にそのデータを外部のデータベースに保存します。
add_action('woocommerce_order_note_added', 'save_note_to_external_db', 10, 2);
function save_note_to_external_db($note_id, $order_id) {
global $wpdb;
$note = get_comment($note_id);
$table_name = $wpdb->prefix . 'external_logs';
$wpdb->insert($table_name, array(
'order_id' => $order_id,
'note_content' => $note->comment_content
));
}
引用元: https://www.example.com