概要
woocommerce_after_set_term_order
はWooCommerceプラグインにおけるアクションフックで、特定のターム(カスタム分類)の順序が設定された後にトリガーされます。このフックは、タームの順序変更処理の後に何らかの追加の処理を実行したい場合に便利です。以下のような機能を実装する際によく使用されます。
- ターム順序の変更後にカスタム通知を追加する。
- タームに関連するメタデータを更新する。
- キャッシュをクリアして新しい順序が反映されるようにする。
- タームの順序変更をログに記録する。
- サードパーティのAPIに新しい順序を送信する。
- タームの順序が変更された場合に管理者にメール通知を送信する。
構文
do_action( 'woocommerce_after_set_term_order', $term_ids, $taxonomy, $args );
パラメータ
$term_ids
(array) – 変更されたタームのIDの配列。$taxonomy
(string) – 操作中のタクソノミーのスラッグ。$args
(array) – その他の引数。
戻り値
このアクションフック自体は戻り値を持ちません。
使用可能なプラグインとバージョン
- WooCommerceバージョン: 4.0以降
- WordPressバージョン: 5.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_after_set_term_order', 'custom_message_after_term_order', 10, 3 );
function custom_message_after_term_order( $term_ids, $taxonomy, $args ) {
echo '<div class="notice notice-success">タームの順序が変更されました!</div>';
}
出典: WordPress Codex
サンプル2: タームに関連するメタデータを更新
タームの順序が変更された後に、そのタームに関連するメタデータを更新する例です。
add_action( 'woocommerce_after_set_term_order', 'update_term_meta_after_order_change', 10, 3 );
function update_term_meta_after_order_change( $term_ids, $taxonomy, $args ) {
foreach ( $term_ids as $term_id ) {
update_term_meta( $term_id, 'last_ordered', current_time( 'mysql' ) );
}
}
出典: WooCommerce Documentation
サンプル3: タームの順序変更をログに記録
この例では、タームの順序変更をエラーログに記録します。
add_action( 'woocommerce_after_set_term_order', 'log_term_order_change', 10, 3 );
function log_term_order_change( $term_ids, $taxonomy, $args ) {
error_log( 'タームの順序が変更されました: ' . implode( ', ', $term_ids ) );
}
出典: WordPress Forums
サンプル4: ターム順序変更後に管理者にメール通知
このコードは、タームの順序が変更された際に管理者にメール通知を送信します。
add_action( 'woocommerce_after_set_term_order', 'notify_admin_after_term_order', 10, 3 );
function notify_admin_after_term_order( $term_ids, $taxonomy, $args ) {
$admin_email = get_option( 'admin_email' );
$subject = 'タームの順序が変更されました';
$message = '変更されたタームID: ' . implode( ', ', $term_ids );
wp_mail( $admin_email, $subject, $message );
}
出典: Stack Overflow
サンプル5: キャッシュをクリアして新しい順序を反映
タームの順序が変更された後に関連するキャッシュをクリアする例です。
add_action( 'woocommerce_after_set_term_order', 'clear_related_cache_after_order_change', 10, 3 );
function clear_related_cache_after_order_change( $term_ids, $taxonomy, $args ) {
// キャッシュクリア処理
wp_cache_flush();
}
出典: WPBeginner