概要
woocommerce_account_$KEY_endpoint
アクションは、WooCommerceのアカウントページにカスタムエンドポイントを追加する際に使用されるフックです。このアクションを使用することで、ユーザーのアカウントセクションに独自の情報や機能を追加することができます。よく使われる機能は以下の通りです。
- ユーザーのプロフィール情報の表示
- 過去の注文の詳細表示
- 顧客向けの特別な割引情報の提供
- ユーザー向けのカスタムメッセージの表示
- ユーザーがサブスクリプションを管理できるページの作成
- ユーザーのアカウントデータを輸出する機能の実装
構文
add_action( 'woocommerce_account_$KEY_endpoint', 'your_function_name' );
パラメータ
$KEY
: エンドポイントのスラッグを表します。your_function_name
: コールバック関数の名前。ここに表示するコンテンツを出力するロジックを実装します。
戻り値
このアクションは特に戻り値を持ちません。コールバック関数内でコンテンツを出力します。
使用可能なプラグインWooCommerceのバージョン
WooCommerceバージョン3.6以降で利用可能です。
ワードプレスのバージョン
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_account_profile_endpoint', 'show_user_profile_info' );
function show_user_profile_info() {
$current_user = wp_get_current_user();
echo '<h3>' . esc_html( $current_user->display_name ) . '</h3>';
echo '<p>' . esc_html( $current_user->user_email ) . '</p>';
}
このコードは、WooCommerceのアカウントページにユーザーのプロフィール情報を表示します。
サンプルコード2: 登録済みの購入履歴の表示
add_action( 'woocommerce_account_orders_endpoint', 'show_purchase_history' );
function show_purchase_history() {
$customer_orders = wc_get_orders( array( 'customer_id' => get_current_user_id() ) );
foreach ( $customer_orders as $order ) {
echo '<p>注文番号: ' . esc_html( $order->get_id() ) . '</p>';
}
}
このコードは、現ユーザーの過去の注文履歴を一覧表示します。
サンプルコード3: カスタム割引情報の表示
add_action( 'woocommerce_account_discount_endpoint', 'show_discount_info' );
function show_discount_info() {
echo '<p>あなた専用の割引コード: DISCOUNT2023</p>';
}
このコードは、特定のユーザーに適用される割引コードをアカウントページに表示します。
サンプルコード4: カスタムメッセージの表示
add_action( 'woocommerce_account_custom_message_endpoint', 'show_custom_message' );
function show_custom_message() {
echo '<h3>ようこそ、特別なゲスト</h3>';
}
このコードは、ユーザーに対してカスタムメッセージを表示します。
サンプルコード5: サブスクリプション管理ページ
add_action( 'woocommerce_account_subscriptions_endpoint', 'manage_subscriptions' );
function manage_subscriptions() {
echo '<h3>サブスクリプション管理</h3>';
// サブスクリプション管理のロジックをここに実装
}
このコードは、ユーザーにサブスクリプションを管理するためのページを提供します。