概要
woocommerce_order_details_before_order_table
はWooCommerceのフックで、顧客の注文詳細ページにおいて、注文テーブルが表示される前にカスタムコンテンツを挿入するために使用されます。このアクションは、管理者や開発者が顧客のための特別な情報を表示したい場合に便利です。例えば、以下のような機能を実装する際によく使われます。
- 注文に関するカスタムメッセージの表示
- 特定のプロモーションや割引情報の提示
- 注文に関連する特典情報の表示
- カスタマイズされたFAQセクションの追加
- 関連商品の推奨表示
- 外部リンクやリソースへの案内
構文
do_action( 'woocommerce_order_details_before_order_table', $order );
パラメータ
$order
: 現在の注文オブジェクト。ウィジェットから取得可能なデータを提供します。
戻り値
このアクションは戻り値を持ちません。純粋にフックとして機能し、他の関数に内容を追加することができます。
使用可能なバージョン
- WooCommerce: 2.1.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_action( 'woocommerce_order_details_before_order_table', 'custom_order_message' );
function custom_order_message( $order ) {
echo '<p>特別なお知らせ: ご注文ありがとうございます!</p>';
}
引用元: WooCommerce公式ドキュメント
サンプル2: プロモーションバナーの追加
このサンプルでは、注文詳細ページにプロモーションバナーを表示します。
add_action( 'woocommerce_order_details_before_order_table', 'custom_promotion_banner' );
function custom_promotion_banner( $order ) {
echo '<div class="promo-banner">次回の注文で10%オフ!クーポンコード: 10OFF</div>';
}
引用元: WooCommerce公式フォーラム
サンプル3: 関連商品セクションの追加
このサンプルでは、関連商品の情報を表示します。
add_action( 'woocommerce_order_details_before_order_table', 'related_products_section' );
function related_products_section( $order ) {
echo '<h3>関連商品</h3>';
// ここに関連商品のロジックを追加
}
引用元: Easy Digital Downloads公式ドキュメント
サンプル4: 注文へのカスタムノートの表示
このサンプルコードでは、注文詳細ページにカスタムノートを表示します。
add_action( 'woocommerce_order_details_before_order_table', 'custom_order_note' );
function custom_order_note( $order ) {
echo '<h4>ご注意</h4>';
echo '<p>この商品は取り扱いにご注意ください。</p>';
}
引用元: Stack Overflow
サンプル5: FAQセクションの追加
このサンプルでは、注文詳細ページにカスタマイズされたFAQセクションを追加します。
add_action( 'woocommerce_order_details_before_order_table', 'custom_faq_section' );
function custom_faq_section( $order ) {
echo '<h4>FAQ</h4>';
echo '<p>商品に関するよくある質問をここに追加できます。</p>';
}
引用元: WordPress Codex