概要
woocommerce_email_order_meta
は、WooCommerceでの注文確認メールをカスタマイズするためのフックです。このアクションを使用することで、注文メールに追加の情報を埋め込むことが可能です。具体的には、次のような機能を実装する際によく使われます。
- 注文に関連するカスタムフィールドの表示
- 特定のプロモーションコードや割引の詳細を追加
- 発送状況や追跡番号の添付
- ユーザーからのメッセージやコメントを含める
- カスタム商品の詳細を表示
- サポート情報や関連商品の提案を加える
構文
add_action( 'woocommerce_email_order_meta', 'custom_function_name', 10, 3 );
パラメータ
$order_id
(int): 注文のID$order
(WC_Order): WC_Orderオブジェクト$email_id
(string): メールの種類(例:’customer_invoice’)
戻り値
このアクション自体は何も戻り値を返しませんが、メールに追加されたコンテンツが表示されます。
使用可能なWooCommerceのバージョン
WooCommerce 2.0以上
使用可能なWordPressのバージョン
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_order_meta', 'display_custom_field', 10, 3 );
function display_custom_field( $order_id, $order, $email_id ) {
$custom_field_value = get_post_meta( $order_id, '_custom_field', true );
if ( !empty( $custom_field_value ) ) {
echo '<p><strong>Custom Field:</strong> ' . esc_html( $custom_field_value ) . '</p>';
}
}
このコードは注文メールにカスタムフィールドの値を表示します。
サンプル2: 割引コードの表示
add_action( 'woocommerce_email_order_meta', 'display_discount_code', 10, 3 );
function display_discount_code( $order_id, $order, $email_id ) {
if ( $order->get_discount_code() ) {
echo '<p><strong>Discount Code:</strong> ' . esc_html( $order->get_discount_code() ) . '</p>';
}
}
このサンプルでは、使用された割引コードをメールに追加します。
サンプル3: 追跡番号を追加
add_action( 'woocommerce_email_order_meta', 'add_tracking_number', 10, 3 );
function add_tracking_number( $order_id, $order, $email_id ) {
$tracking_number = get_post_meta( $order_id, '_tracking_number', true );
if ( !empty( $tracking_number ) ) {
echo '<p><strong>Tracking Number:</strong> ' . esc_html( $tracking_number ) . '</p>';
}
}
このコードは注文に関連する追跡番号を表示します。
サンプル4: ユーザーからのメッセージを表示
add_action( 'woocommerce_email_order_meta', 'display_user_message', 10, 3 );
function display_user_message( $order_id, $order, $email_id ) {
$user_message = get_post_meta( $order_id, '_user_message', true );
if ( !empty( $user_message ) ) {
echo '<p><strong>User Message:</strong> ' . esc_html( $user_message ) . '</p>';
}
}
この例では、ユーザーが注文時に残したメッセージを表示します。
サンプル5: 関連商品の提案
add_action( 'woocommerce_email_order_meta', 'suggest_related_products', 10, 3 );
function suggest_related_products( $order_id, $order, $email_id ) {
$related_products = get_post_meta( $order_id, '_related_products', true );
if ( !empty( $related_products ) ) {
echo '<p><strong>Related Products:</strong> ' . esc_html( implode( ', ', $related_products ) ) . '</p>';
}
}
このコードは注文に関連する商品の提案をメールに追加します。