概要
woocommerce_email_customer_details
は、WooCommerceにおけるメールテンプレート内で顧客の詳細情報を表示するためのフックです。このアクションを使うことで、ユーザーに送信されるメールに追加の顧客情報やカスタムデータを挿入することができます。主に以下のような用途で使用されます:
- 顧客の追加情報を表示する
- 注文に関連する特殊メッセージを挿入する
- カスタムフィールドの情報をメールに表示する
- ロイヤルティプログラムやプロモーション情報を案内する
- カスタマーサポート用の連絡先情報を表示する
- マーケティングメッセージを挿入する
構文
add_action( 'woocommerce_email_customer_details', 'your_function', 10, 4 );
パラメータ
$order
: 現在の注文オブジェクト$sent_to_admin
: 管理者に送信されたかどうか$plain_text
: プレーンテキストかどうか$email
: 現在のメールオブジェクト
戻り値
このアクションは、値を返すことはありません。主に方法を使用して副作用を持つ目的で使用します。
使用可能なプラグインWooCommerceのバージョン
WooCommerce 2.4以降
ワードプレスのバージョン
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_action( 'woocommerce_email_customer_details', 'add_customer_phone_to_email', 10, 4 );
function add_customer_phone_to_email( $order, $sent_to_admin, $plain_text, $email ) {
echo '<p><strong>' . __( 'Phone:', 'your-text-domain' ) . '</strong> ' . get_post_meta( $order->get_id(), '_billing_phone', true ) . '</p>';
}
出典: WooCommerce documentation
サンプルコード 2
顧客からのメモをメールに表示するサンプルです。
add_action( 'woocommerce_email_customer_details', 'add_customer_note_to_email', 10, 4 );
function add_customer_note_to_email( $order, $sent_to_admin, $plain_text, $email ) {
$customer_note = get_post_meta( $order->get_id(), '_customer_note', true );
if ( ! empty( $customer_note ) ) {
echo '<p><strong>' . __( 'Customer Note:', 'your-text-domain' ) . '</strong> ' . esc_html( $customer_note ) . '</p>';
}
}
出典: WooCommerce documentation
サンプルコード 3
カスタムフィールド情報をメールに追加するサンプルです。
add_action( 'woocommerce_email_customer_details', 'add_custom_field_to_email', 10, 4 );
function add_custom_field_to_email( $order, $sent_to_admin, $plain_text, $email ) {
$custom_field = get_post_meta( $order->get_id(), '_custom_field_key', true );
if ( ! empty( $custom_field ) ) {
echo '<p><strong>' . __( 'Custom Field:', 'your-text-domain' ) . '</strong> ' . esc_html( $custom_field ) . '</p>';
}
}
出典: WooCommerce documentation
サンプルコード 4
プロモーション情報を顧客に提供するサンプルです。
add_action( 'woocommerce_email_customer_details', 'add_promotional_message_to_email', 10, 4 );
function add_promotional_message_to_email( $order, $sent_to_admin, $plain_text, $email ) {
echo '<p>' . __( 'Thank you for your order! Use code "SAVE10" for 10% off your next purchase.', 'your-text-domain' ) . '</p>';
}
出典: WooCommerce documentation
サンプルコード 5
顧客サポート用の連絡先情報を表示するサンプルです。
add_action( 'woocommerce_email_customer_details', 'add_support_contact_to_email', 10, 4 );
function add_support_contact_to_email( $order, $sent_to_admin, $plain_text, $email ) {
echo '<p>' . __( 'For any inquiries, please contact support at support@example.com.', 'your-text-domain' ) . '</p>';
}
出典: WooCommerce documentation