概要
woocommerce_allow_switching_email_locale
フィルタは、WooCommerce のメール送信時にロケール(言語)を切り替えることを許可するために使用されます。このフィルタを使用することで、ユーザーの設定や注文の状況に応じて、適切な言語でのメール通知を提供できます。具体的には以下のような機能の実装時によく使われます:
- マルチサイト環境での異なる言語の管理
- ユーザーの地域に応じたメールの言語を選択
- 特定のユーザーグループに対して異なる言語での通知
- 地域ごとのセール情報の提供
- 国際化されたビジネスでの顧客対応
- 商品のローカライズ戦略の一環
構文
add_filter( 'woocommerce_allow_switching_email_locale', 'your_function_name', 10, 2 );
パラメータ
$allow
(bool): メール送信時に言語切替を許可するかどうかのフラグ。$order
(WC_Order): 対象となる注文オブジェクト。
戻り値
- bool: 言語切替を許可する場合は
true
、しない場合はfalse
を返す。
使用可能なプラグインWooCommerceのバージョン
このフィルタは WooCommerce バージョン 2.6 以降で使用可能です。
使用可能なワードプレスのバージョン
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_allow_switching_email_locale', function( $allow, $order ) {
if ( 'JP' === $order->get_billing_country() ) {
$allow = true; // 日本のお客様に対してはメールのロケールを切り替え可能に
}
return $allow;
});
このコードは、注文の請求国が日本の場合にのみメールのロケール切替を許可します。
サンプルコード 2
add_filter( 'woocommerce_allow_switching_email_locale', function( $allow, $order ) {
// 特定のメタデータを持つ注文に対してのみ許可
if ( $order->get_meta( '_requires_special_locale' ) === 'yes' ) {
$allow = true;
}
return $allow;
});
特定のメタデータに基づいてメールのロケール切替を許可するサンプルです。
サンプルコード 3
add_filter( 'woocommerce_allow_switching_email_locale', function( $allow, $order ) {
// ユーザーの役割によって切り替える
if ( in_array( 'wholesale_customer', $order->get_user()->roles ) ) {
$allow = true; // 卸売顧客の場合
}
return $allow;
});
特定のユーザーロールを持つ顧客に対してメールのロケール切替を許可します。
サンプルコード 4
add_filter( 'woocommerce_allow_switching_email_locale', function( $allow, $order ) {
// 注文日が特定の日付以降であれば許可
if ( strtotime( $order->get_date_created() ) > strtotime( '2023-01-01' ) ) {
$allow = true;
}
return $allow;
});
特定の日付以降に作成された注文に対してメールのロケール切替を許可します。
サンプルコード 5
add_filter( 'woocommerce_allow_switching_email_locale', function( $allow, $order ) {
// 特定の商品の注文に対してのみ許可
if ( $order->has_product( 123 ) ) { // 商品ID 123
$allow = true;
}
return $allow;
});
特定の商品が含まれる注文に対してメールのロケール切替を許可するコードです。