プラグインWooCommerceのwoocommerce_order_details_after_order_table_itemsアクションの使用方法・解説

概要

woocommerce_order_details_after_order_table_items アクションは、WooCommerceの注文詳細ページにおいて、注文アイテムテーブルの後ろにカスタムコンテンツを追加するためのフックです。このアクションは、以下のような機能を実装する際によく使われます。

  1. カスタムメッセージの表示
  2. 注文に関する注意事項の追加
  3. 関連商品の推薦
  4. ユーザーの次回購入を促す情報提供
  5. 注文履歴の惹きつけるビジュアル要素の追加
  6. 支払いのリマインダーやクーポンコードの表示

構文

do_action( 'woocommerce_order_details_after_order_table_items', $order );

パラメータ

  • $order (WC_Order): 現在の注文オブジェクト。

戻り値

  • 特になし。このアクションは何かを返すものではありません。

使用可能なバージョン

  • WooCommerce: 3.0.0 以降
  • WordPress: 4.0 以降

サンプルコード

サンプルコード 1

add_action( 'woocommerce_order_details_after_order_table_items', 'display_custom_message_after_order_items' );

function display_custom_message_after_order_items( $order ) {
    echo '<p>ありがとうございます!ご注文を心より感謝申し上げます。</p>';
}

このサンプルは、注文詳細のアイテムテーブルの後に顧客への感謝メッセージを表示するものです。

サンプルコード 2

add_action( 'woocommerce_order_details_after_order_table_items', 'show_related_products' );

function show_related_products( $order ) {
    $related_products = wc_get_related_products( $order->get_id(), 2 );
    if ( $related_products ) {
        echo '<h3>あなたにおすすめの商品</h3>';
        foreach ( $related_products as $product_id ) {
            $product = wc_get_product( $product_id );
            echo '<p>' . $product->get_name() . '</p>';
        }
    }
}

このサンプルは、注文後に関連商品を表示する機能を追加します。

サンプルコード 3

add_action( 'woocommerce_order_details_after_order_table_items', 'add_order_notes' );

function add_order_notes( $order ) {
    if ( $order->get_customer_note() ) {
        echo '<h3>お客様からのメモ</h3>';
        echo '<p>' . esc_html( $order->get_customer_note() ) . '</p>';
    }
}

このサンプルは、注文に顧客が残したメモを表示します。

サンプルコード 4

add_action( 'woocommerce_order_details_after_order_table_items', 'show_discount_offer' );

function show_discount_offer( $order ) {
    echo '<p>次回のお買い物で使える10%オフクーポンコード: DISCOUNT10</p>';
}

このサンプルは、次回購入に使える割引クーポンコードを注文詳細に表示します。

サンプルコード 5

add_action( 'woocommerce_order_details_after_order_table_items', 'display_shipping_info' );

function display_shipping_info( $order ) {
    $shipping_address = $order->get_shipping_address_1();
    echo '<h3>配送先情報</h3>';
    echo '<p>' . esc_html( $shipping_address ) . '</p>';
}

このサンプルは、注文詳細の後に配送先情報を表示するものです。

この関数のアクションでの使用可能性

アクション 使用例
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

この表は、woocommerce_order_details_after_order_table_items アクションが他のアクションフックでどのように活用されているかを示したものです。大抵の場合、このアクションは主にWooCommerce特有のものなので、WordPressコアの多くのアクションでは使用されていません。

この関数について質問する


上の計算式の答えを入力してください