概要
woocommerce_email_after_order_table は、WooCommerceのメールテンプレート内で注文情報のテーブルの後に特定のコンテンツを追加するためのフックです。このアクションは、顧客への確認メールや、管理者の通知メールなど、さまざまな場面で利用されます。具体的には以下のような機能を実装する際によく使われます:
- カスタムメッセージの追加
- プロモーションや割引コードの提供
- 注文に関連するFAQのリンク追加
- 送信元の連絡先情報を掲載
- 顧客へのおすすめ商品を表示
- 注文内容の特別な注意点を強調提示
構文
add_action('woocommerce_email_after_order_table', 'custom_function_name', 10, 4);
パラメータ
$order: WC_Order オブジェクト – 注文データ$sent_to_admin: boolean – 管理者へ送信されているかどうか$plain_text: boolean – プレーンテキストメールかどうか$email: WC_Email オブジェクト – メールの種類
戻り値
このフック自体に戻り値はありませんが、追加される内容がメールに影響します。
使用可能なプラグインバージョン
WooCommerceのバージョン5.0以降で使用可能。
使用可能なWordPressバージョン
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_action('woocommerce_email_after_order_table', 'add_custom_message_to_email', 10, 4);
function add_custom_message_to_email($order, $sent_to_admin, $plain_text, $email) {
echo '<p>ご注文ありがとうございます!</p>';
}
出典: https://docs.woocommerce.com/document/woocommerce-email-hooks/
サンプル2: 割引コードの提案
このコードは、メール内に次回使える割引コードを追加します。
add_action('woocommerce_email_after_order_table', 'add_discount_code_to_email', 10, 4);
function add_discount_code_to_email($order, $sent_to_admin, $plain_text, $email) {
echo '<p>次回のご購入時に使える割引コード:DISCOUNT10</p>';
}
出典: https://woofunnels.com
サンプル3: おすすめ商品の表示
このコードは、メールに関連商品のリストを追加します。
add_action('woocommerce_email_after_order_table', 'suggest_related_products', 10, 4);
function suggest_related_products($order, $sent_to_admin, $plain_text, $email) {
// サンプルコード:特定の商品IDを指定して関連商品を取得
$related_products = wc_get_related_products($order->get_items()[0]['product_id']);
foreach($related_products as $product_id) {
$product = wc_get_product($product_id);
echo '<p>おすすめ商品: ' . $product->get_name() . '</p>';
}
}
出典: https://www.hostinger.com/tutorials/how-to-create-a-custom-woocommerce-email
サンプル4: 連絡先情報の追加
このコードは、カスタマーサポートの連絡先情報をメールに加えます。
add_action('woocommerce_email_after_order_table', 'add_contact_info', 10, 4);
function add_contact_info($order, $sent_to_admin, $plain_text, $email) {
echo '<p>サポートが必要な場合は、info@example.com までご連絡ください。</p>';
}
出典: https://www.businessbloomer.com/woocommerce-custom-email-hooks/
サンプル5: FAQのリンクを追加
このコードは、受注メールにFAQのリンクを挿入します。
add_action('woocommerce_email_after_order_table', 'add_faq_link', 10, 4);
function add_faq_link($order, $sent_to_admin, $plain_text, $email) {
echo '<p>よくある質問は <a href="https://example.com/faq">こちら</a> をご覧ください。</p>';
}
出典: https://kinsta.com/blog/woocommerce-email-templates/#customizing-woocommerce-emails
これらのサンプルコードを参考にして、WooCommerceでメール内容をカスタマイズすることができます。