概要
woocommerce_email_header
は、WooCommerceのメールテンプレートに対してカスタムヘッダーを追加するためのフックです。このアクションは、メールの送信時に特定の内容やスタイルを挿入するために使用されます。具体的な用途としては、次のような場面での利用が考えられます。
- メールのデザインをカスタマイズする。
- ブランドのロゴを自動で挿入する。
- 特定のスタイルやCSSをメールに追加する。
- ユーザー名や注文情報などの動的データを挿入する。
- メールの体裁を整えるためのHTML構造を変更する。
- メールの内容に独自のテキストやリンクを追加する。
構文
add_action( 'woocommerce_email_header', 'custom_email_header_function', 10, 2 );
パラメータ
$email_id
: 送信されるメールの識別子。$email
: 送信されたメールオブジェクト。
戻り値
このアクションには戻り値はありません。カスタマイズしたヘッダーがダイレクトにメールに適用されます。
使用可能なバージョン
- WooCommerce バージョン: 3.0 以降
- WordPress バージョン: 4.0 以降
サンプルコード
サンプル1: ブランドロゴの追加
このコードは、WooCommerceのメールにブランドロゴを追加します。
add_action( 'woocommerce_email_header', 'add_brand_logo_to_email', 10, 2 );
function add_brand_logo_to_email( $email_id, $email ) {
echo '<img src="https://example.com/path/to/logo.png" alt="Brand Logo" style="width: 100%; max-width: 600px;">';
}
引用元: https://woocommerce.com/document/woocommerce-email-filters/
サンプル2: メールヘッダーにカスタムメッセージを追加
このコードは、メールのヘッダーにカスタムメッセージを追加します。
add_action( 'woocommerce_email_header', 'add_custom_message_to_header', 10, 2 );
function add_custom_message_to_header( $email_id, $email ) {
if ( $email_id === 'customer_completed_order' ) {
echo '<p>ご注文ありがとうございます!</p>';
}
}
引用元: https://woocommerce.com/document/woocommerce-email-filters/
サンプル3: メールヘッダーのスタイルを変更
このコードは、メールヘッダーにスタイルを追加します。
add_action( 'woocommerce_email_header', 'style_email_header', 10, 2 );
function style_email_header( $email_id, $email ) {
echo '<style>h1{color: blue;}</style><h1>ご注文確認</h1>';
}
引用元: https://woocommerce.com/document/woocommerce-email-filters/
サンプル4: 特定の条件でのカスタムヘッダー内容
このコードは、特定の条件に基づいて異なるヘッダーをメールに追加します。
add_action( 'woocommerce_email_header', 'conditional_email_header', 10, 2 );
function conditional_email_header( $email_id, $email ) {
if ( $email_id === 'customer_invoice' ) {
echo '<h2>お支払い明細書</h2>';
} else {
echo '<h2>ご注文リスト</h2>';
}
}
引用元: https://woocommerce.com/document/woocommerce-email-filters/
サンプル5: ユーザー情報の挿入
このコードは、ヘッダーに現在のユーザーの名前を追加します。
add_action( 'woocommerce_email_header', 'insert_user_name_in_email_header', 10, 2 );
function insert_user_name_in_email_header( $email_id, $email ) {
$user = wp_get_current_user();
if ( $user ) {
echo '<p>こんにちは、' . esc_html( $user->display_name ) . 'さん。ご注文いただきありがとうございます。</p>';
}
}
引用元: https://woocommerce.com/document/woocommerce-email-filters/
この関数のアクションでの使用可能性
フック | 使用例 |
---|---|
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 |