プラグインWooCommerceのwc_payment_gateway_enabled_notification_email_addressesフィルタの使用方法・解説

概要

wc_payment_gateway_enabled_notification_email_addresses フィルタは、WooCommerce プラグインで利用され、支払いゲートウェイが有効になった際に通知を送信する対象のメールアドレスをカスタマイズするために使用されます。このフィルタは、以下のような機能を実装する際によく使われます。

  1. 特定の管理者に通知を送信する。
  2. チームメンバーにリアルタイムの支払い通知を提供する。
  3. 環境によって異なるメールアドレスを設定する(例: 開発環境 vs 本番環境)。
  4. 支払いゲートウェイ別に通知先を分ける。
  5. フィルタリングや条件に基づいたメール送信を実施する。
  6. ユーザーの役割に応じて異なる通知先を設定する。

構文

apply_filters( 'wc_payment_gateway_enabled_notification_email_addresses', $email_addresses );

パラメータ

  • $email_addresses (array): 通知を送信するメールアドレスの配列。

戻り値

  • array: カスタマイズされたメールアドレスの配列。

使用可能な WooCommerce バージョン

このフィルタは、WooCommerce バージョン 3.0 以降で使用可能です。

使用可能な WordPress バージョン

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_filter( 'wc_payment_gateway_enabled_notification_email_addresses', function( $emails ) {
    $emails[] = 'admin@example.com'; // 管理者用のメールアドレスを追加
    return $emails;
});

このコードは、管理者のメールアドレスを通知先に追加します。

サンプル 2: 環境に応じたメールアドレスの設定

add_filter( 'wc_payment_gateway_enabled_notification_email_addresses', function( $emails ) {
    if ( defined('WP_ENV') && WP_ENV === 'production' ) {
        $emails[] = 'prod@example.com';
    } else {
        $emails[] = 'dev@example.com';
    }
    return $emails;
});

このコードは、環境変数に基づいて異なるメールアドレスを設定します。

サンプル 3: 特定のユーザー役割に基づくメール送信

add_filter( 'wc_payment_gateway_enabled_notification_email_addresses', function( $emails ) {
    if ( current_user_can( 'administrator' ) ) {
        $emails[] = 'admin@example.com';
    }
    return $emails;
});

このコードは、現在のユーザーが管理者である場合にのみ、管理者用のメールアドレスを通知先に追加します。

サンプル 4: 支払いゲートウェイ別の設定

add_filter( 'wc_payment_gateway_enabled_notification_email_addresses', function( $emails ) {
    if ( isset($_POST['payment_gateway']) && $_POST['payment_gateway'] === 'stripe' ) {
        $emails[] = 'stripe-team@example.com'; // Stripe用のチームメールアドレス
    }
    return $emails;
});

このコードは、支払いゲートウェイがStripeの場合に特定のメールアドレスを追加します。

サンプル 5: 複数のメールアドレスを設定

add_filter( 'wc_payment_gateway_enabled_notification_email_addresses', function( $emails ) {
    $emails = array_merge( $emails, ['team@example.com', 'support@example.com'] ); // 複数のメールアドレスを追加
    return $emails;
});

このコードは、複数のメールアドレスを一度に追加します。

以上のサンプルコードは、すべて著作権フリーの例であり、ユーザーのニーズに応じたカスタマイズに役立ちます。

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


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