プラグインWooCommerceのwoocommerce_payment_token_deletedアクションの使用方法・解説

概要

woocommerce_payment_token_deleted アクションは、WooCommerceプラグインにおいて、支払いトークンが削除された際に実行されるフックです。このアクションは主に、支払い情報や顧客データを管理する際に使用されます。具体的には以下の6つの機能を実装する際によく使用されます。

  1. 顧客の支払い履歴の管理
  2. 支払い情報が削除されたときの通知機能
  3. カスタムロギング機能の実装
  4. 統計データの収集や分析
  5. セキュリティ上の監査ログの保存
  6. 他のエクステンションとの連携機能の実装

構文

do_action( 'woocommerce_payment_token_deleted', $token_id );

パラメータ

  • $token_id (int): 削除された支払いトークンのID。

戻り値

このアクション自体は戻り値を持ちません。

使用可能なプラグインWooCommerceのバージョン

  • WooCommerce 2.6.0以降

使用可能なワードプレスのバージョン

  • WordPress 4.7以降

この関数のアクションでの使用可能性

アクション名 使用例
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_payment_token_deleted', 'log_payment_token_deletion' );

function log_payment_token_deletion( $token_id ) {
    $log_entry = sprintf( '支払いトークン %d が削除されました。', $token_id );
    error_log( $log_entry );
}

このコードは、支払いトークンが削除されると、そのトークンIDをエラーログに記録します。

サンプルコード2: 削除通知メールの送信

add_action( 'woocommerce_payment_token_deleted', 'send_token_deletion_email' );

function send_token_deletion_email( $token_id ) {
    $user_email = wp_get_current_user()->user_email;
    $subject = '支払いトークン削除通知';
    $message = sprintf( 'あなたの支払いトークン %d が削除されました。', $token_id );
    wp_mail( $user_email, $subject, $message );
}

この例では、支払いトークンが削除された際に、ユーザーに通知メールを送信します。

サンプルコード3: カスタムアクションの追加

add_action( 'woocommerce_payment_token_deleted', 'custom_action_on_token_deletion' );

function custom_action_on_token_deletion( $token_id ) {
    do_action( 'my_custom_action', $token_id );
}

このコードは、支払いトークンが削除されるとカスタムアクションを実行します。

サンプルコード4: 支払いトークンの削除処理ログ

add_action( 'woocommerce_payment_token_deleted', 'record_deletion_process' );

function record_deletion_process( $token_id ) {
    $deletion_time = current_time( 'mysql' );
    $log_entry = sprintf( 'トークン %d が %s に削除されました。', $token_id, $deletion_time );
    file_put_contents( '/path/to/custom_log.txt', $log_entry, FILE_APPEND );
}

この例では、支払いトークンの削除処理を特定のファイルに記録します。

サンプルコード5: データベースのカスタムテーブルへの記録

add_action( 'woocommerce_payment_token_deleted', 'save_to_custom_table' );

function save_to_custom_table( $token_id ) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'custom_log';
    $wpdb->insert( 
        $table_name, 
        array( 
            'token_id' => $token_id, 
            'deleted_at' => current_time( 'mysql' ),
        ) 
    );
}

このコードは、支払いトークンが削除された情報をカスタムデータベーステーブルに記録します。

この関数について質問する


上の計算式の答えを入力してください