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

概要

フィルタ woocommerce_email_subject_customer_invoice は、WooCommerceで顧客に送信される請求書メールの件名を変更するために使用されるフックです。このフックを使うことで、店舗運営者は請求書のメール件名を顧客に合わせて調整することができます。たとえば、特定のプロモーションや顧客のニーズに応じた情報を件名に盛り込むことで、メールの開封率を向上させることが可能です。

このフィルタは、以下のようなシナリオでよく使われます:
1. メールの件名をブランド名とともにカスタマイズする
2. 特定の商品の購買時に関連する件名を追加する
3. セールやプロモーションの情報を件名に含める
4. 送付先顧客の名前を件名に追加する
5. 定期購読やサブスクリプションに対する請求書の件名を特別扱いする
6. 請求書に関連する特定のメッセージを件名に加える

構文

add_filter('woocommerce_email_subject_customer_invoice', 'custom_invoice_subject', 10, 2);

パラメータ

  • string $subject: 既存の件名
  • WC_Order $order: 対象となる注文オブジェクト

戻り値

  • 変更後の件名:文字列形式

対応バージョン

  • WooCommerce: 5.0以上推奨
  • WordPress: 5.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_subject_customer_invoice', 'add_brand_name_to_invoice_subject', 10, 2);
function add_brand_name_to_invoice_subject($subject, $order) {
    $brand_name = 'My Shop'; // ブランド名
    return $brand_name . ': ' . $subject;
}

このサンプルコードは、請求書の件名に「My Shop」というブランド名を追加しています。

サンプル2: 顧客名を件名に追加する

add_filter('woocommerce_email_subject_customer_invoice', 'add_customer_name_to_invoice_subject', 10, 2);
function add_customer_name_to_invoice_subject($subject, $order) {
    $customer_name = $order->get_billing_first_name();
    return '請求書 - ' . $customer_name . ' 様のご注文';
}

このサンプルでは、顧客の名を請求書の件名に盛り込んで、個別性を持たせています。

サンプル3: セール情報を件名に含める

add_filter('woocommerce_email_subject_customer_invoice', 'add_sale_info_to_invoice_subject', 10, 2);
function add_sale_info_to_invoice_subject($subject, $order) {
    return $subject . ' - 特別セール中!';
}

このコードは、請求書の件名に「特別セール中!」という文言を追加します。

サンプル4: 特定の商品名を件名に加える

add_filter('woocommerce_email_subject_customer_invoice', 'add_product_name_to_invoice_subject', 10, 2);
function add_product_name_to_invoice_subject($subject, $order) {
    $items = $order->get_items();
    $product_names = [];
    foreach ($items as $item_id => $item) {
        $product_names[] = $item->get_name();
    }
    return $subject . ' - ' . implode(', ', $product_names);
}

このサンプルでは、請求書のメール件名に注文した商品の名前をすべて列挙しています。

サンプル5: メール送信のタイミングによる件名変更

add_filter('woocommerce_email_subject_customer_invoice', 'custom_invoice_subject_based_on_time', 10, 2);
function custom_invoice_subject_based_on_time($subject, $order) {
    $current_hour = date('H');
    if ($current_hour < 12) {
        return 'おはようございます!' . $subject;
    } else {
        return 'こんばんは!' . $subject;
    }
}

このサンプルコードは、時間によって請求書のメール件名を変更します。「おはようございます!」または「こんばんは!」が件名に加わります。

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


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