概要
woocommerce_email_enabled_$THIS->ID
フィルタは WooCommerce プラグインで使用されるフィルタの一つで、特定のメール通知の有効・無効を制御するために使用されます。このフィルタは、メールの種類ごとに異なる $THIS->ID
を指定することにより、特定のメールが送信されるかどうかを決定します。以下は、このフィルタがよく使われるシナリオの例です。
- 注文確認メールの有効・無効の制御
- 顧客への発送通知メールの制御
- 返品リクエストの通知メールの設定
- 定期購読のリマインダーメールの送信制御
- 在庫切れ通知メールの有効化・無効化
- 取引完了メールの配信設定
このフィルタの構文は次の通りです:
apply_filters( 'woocommerce_email_enabled_$THIS->ID', $enabled, $order, $email );
パラメータ
$enabled
(bool): メールが有効であれば true、無効であれば false。$order
(WC_Order): 対象の注文オブジェクト。$email
(WC_Email): 対象のメールオブジェクト。
戻り値
- bool: メールが有効かどうかを示す。
使用可能なバージョン
- WooCommerce: 3.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_filter( 'woocommerce_email_enabled_customer_completed_order', 'disable_completed_order_email', 10, 3 );
function disable_completed_order_email( $enabled, $order, $email ) {
if ( /* 何らかの条件 */ ) {
return false; // メールを無効化
}
return $enabled; // メールを有効化
}
このコードは、特定の条件に基づいて注文完了メールを無効化します。
サンプルコード 2
add_filter( 'woocommerce_email_enabled_customer_invoice', 'custom_invoice_email', 10, 3 );
function custom_invoice_email( $enabled, $order, $email ) {
// カスタムロジックを追加
return true; // メールを有効化
}
このコードは、請求書メールを常に有効にします。
サンプルコード 3
add_filter( 'woocommerce_email_enabled_customer_new_account', 'custom_new_account_email', 10, 3 );
function custom_new_account_email( $enabled, $order, $email ) {
if ( user_has_special_role() ) {
return true; // 特定の役割を持つユーザーに対してメールを有効化
}
return false; // その他のユーザーには無効化
}
このコードは、特定のユーザー役割を持つ場合に新規アカウント作成メールを有効化します。
サンプルコード 4
add_filter( 'woocommerce_email_enabled_customer_on_hold_order', 'conditional_hold_order_email', 10, 3 );
function conditional_hold_order_email( $enabled, $order, $email ) {
if ( /* 注文が特定の条件を満たす場合 */ ) {
return false; // メールを無効化
}
return $enabled; // メールを有効にする
}
このコードは、注文が特定の条件に該当する場合に、保留中の注文メールを無効化します。
サンプルコード 5
add_filter( 'woocommerce_email_enabled_customer_failed_payment', 'enable_failed_payment_email_for_members', 10, 3 );
function enable_failed_payment_email_for_members( $enabled, $order, $email ) {
if ( is_user_logged_in() && current_user_can( 'member_role' ) ) {
return true; // メンバーの場合、失敗した支払いメールを有効化
}
return false; // 一般ユーザーには無効化
}
このコードは、ログインしている特定のユーザーロールに対して失敗した支払いメールを有効にします。