概要
woocommerce_email_order_details
は、WooCommerceで送信される注文確認メールの内容をカスタマイズするためのフィルターフックです。このフックを使用することで、メールの中で表示される注文の詳細やその他の情報をカスタマイズすることができます。主に以下のような機能を実装する際に頻繁に使用されます。
- 注文詳細のカスタム情報を追加する
- 商品情報のフォーマットを変更する
- 注文の合計金額に特定の割引を表示する
- 注文に関連するカスタムフィールドを表示する
- メールの言語を変更する
- 購入者向けの特別オファーやキャンペーンを表示する
構文
add_filter( 'woocommerce_email_order_details', 'custom_function', 10, 4 );
function custom_function( $order, $sent_to_admin, $plain_text, $email ) {
// カスタマイズコード
}
パラメータ
$order
— WC_Orderオブジェクト、注文の詳細を含む。$sent_to_admin
— 管理者に送信されたかどうかを示す真偽値。$plain_text
— プレーンテキスト形式かどうかを示す真偽値。$email
— 使用されているメールのタイプ。
戻り値
このフィルターは、メールの内容をカスタマイズした文字列を返します。
互換性
- 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_order_details', 'add_custom_message_to_email', 10, 4 );
function add_custom_message_to_email( $order, $sent_to_admin, $plain_text, $email ) {
echo '<p>ご注文ありがとうございます!新しい商品をチェックしてください。</p>';
}
[出典: WooCommerce Documentation]
サンプルコード2: 商品の価格に割引を追加
このコードは、商品の価格に特定の割引を表示します。
add_filter( 'woocommerce_email_order_details', 'add_discount_to_email', 10, 4 );
function add_discount_to_email( $order, $sent_to_admin, $plain_text, $email ) {
$discount = 10; // 割引額
echo '<p>割引: ¥' . $discount . '</p>';
}
[出典: WPBeginner]
サンプルコード3: 注文にカスタムフィールドを表示
このコードは、注文に関連するカスタムフィールドをメールに追加します。
add_filter( 'woocommerce_email_order_details', 'display_custom_field_in_email', 10, 4 );
function display_custom_field_in_email( $order, $sent_to_admin, $plain_text, $email ) {
$custom_field = get_post_meta( $order->get_id(), '_custom_field', true );
if ( $custom_field ) {
echo '<p>カスタムフィールド: ' . esc_html( $custom_field ) . '</p>';
}
}
[出典: Code Snippets]
サンプルコード4: 注文の合計金額を強調
このコードは、注文の合計金額を太字で表示します。
add_filter( 'woocommerce_email_order_details', 'highlight_order_total_in_email', 10, 4 );
function highlight_order_total_in_email( $order, $sent_to_admin, $plain_text, $email ) {
echo '<p style="font-weight:bold;">合計金額: ' . $order->get_total() . '</p>';
}
[出典: ThemeGrill]
サンプルコード5: 注文確認メールのタイトルを変更
このコードは、注文確認メールのタイトルを変更します。
add_filter( 'woocommerce_email_subject_customer_order', 'change_email_subject', 10, 2 );
function change_email_subject( $subject, $order ) {
return 'あなたの新しい注文 - ご確認ください!';
}
[出典: WPExplorer]
以上のサンプルコードを利用することで、WooCommerceのメール機能をカスタマイズし、顧客にとってより価値のある情報を提供できます。