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

概要

woocommerce_email_heading_customer_invoice フィルタは、WooCommerceが送信する顧客インボイスのメールヘッダーを変更するために使用されるフックです。このフィルタを利用することで、企業や開発者はメールの見出しをカスタマイズし、顧客に対するメッセージのトーンやブランドを強調することができます。

このフィルタは、以下のような機能を実装する際によく使用されます:
1. カスタムブランドメッセージを追加するため
2. メールの見出しを多言語に対応させるため
3. 特定のプロモーションやオファーを強調するため
4. 見積もり書と請求書で異なる見出しを使用するため
5. イベントやキャンペーンに合わせて見出しを変更するため
6. 組織のスタイルガイドに沿った文言で統一するため

構文

add_filter( 'woocommerce_email_heading_customer_invoice', 'custom_invoice_email_heading', 10, 2 );

パラメータ

  • string $heading: 変更するヘッダーのテキスト。
  • WC_Order $order: 現在の注文オブジェクト。

戻り値

このフィルターフックは文字列(新しいヘッダー)を返します。

バージョン

  • WooCommerce: 3.0.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_heading_customer_invoice', 'custom_fixed_invoice_heading', 10, 2 );

function custom_fixed_invoice_heading( $heading, $order ) {
    return 'あなたのインボイス: ' . $order->get_order_number();
}

引用元: https://woocommerce.com

サンプル2: 複数言語対応

この例では、多言語サイト向けにヘッダーを変更します。

add_filter( 'woocommerce_email_heading_customer_invoice', 'custom_multilingual_invoice_heading', 10, 2 );

function custom_multilingual_invoice_heading( $heading, $order ) {
    // 簡単な条件分岐
    if ( get_locale() == 'ja' ) {
        return '支払い請求書';
    }
    return 'Invoice';
}

引用元: https://woocommerce.com

サンプル3: プロモーション情報を追加

このコードは、注文に関連するプロモーション情報をメールヘッダーに追加します。

add_filter( 'woocommerce_email_heading_customer_invoice', 'custom_promotion_invoice_heading', 10, 2 );

function custom_promotion_invoice_heading( $heading, $order ) {
    return $heading . ' - 特別プロモーション中!';
}

引用元: https://woocommerce.com

サンプル4: 特定の条件に基づくカスタマイズ

特定の製品が含まれている場合に、ヘッダーを変更します。

add_filter( 'woocommerce_email_heading_customer_invoice', 'custom_condition_invoice_heading', 10, 2 );

function custom_condition_invoice_heading( $heading, $order ) {
    foreach ( $order->get_items() as $item ) {
        if ( $item->get_product_id() == 123 ) { // 特定の製品ID
            return '特別なインボイス - Product 123を含む';
        }
    }
    return $heading;
}

引用元: https://woocommerce.com

サンプル5: ユーザーの役職に応じたメッセージ変更

ユーザーの役職に応じて、異なるメッセージを設定します。

add_filter( 'woocommerce_email_heading_customer_invoice', 'custom_role_based_invoice_heading', 10, 2 );

function custom_role_based_invoice_heading( $heading, $order ) {
    if ( current_user_can( 'administrator' ) ) {
        return '管理者用インボイス';
    }
    return '通常のインボイス';
}

引用元: https://woocommerce.com

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


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