概要
woocommerce_thankyou_order_key
フィルタは、WooCommerceの注文完了ページにおいて、特定の注文に関連する情報を処理するために使用されるフックです。このフィルタを利用することで、注文完了メッセージや通知、カスタムリダイレクトなどを実装することができます。具体的には、以下のような機能の実装によく用いられます。
- 注文完了ページのカスタマイズ
- カスタムメッセージの表示
- 特定の条件に基づくリダイレクト
- 注文完了後のデータ分析
- 特別オファーやプロモーションの表示
- 第三者サービスへのデータ送信(例:マーケティングツール)
構文
add_filter('woocommerce_thankyou_order_key', 'your_custom_function', 10, 2);
パラメータ
$order_key
(string): フィルタ対象の注文キー。$order_id
(int): 対象の注文ID。
戻り値
- カスタマイズされた注文キー(string)。
WooCommerceバージョン
- 5.x以降(具体的なバージョンによる差異はなし)。
WordPressバージョン
- WordPress 5.x以降(同様に、具体的なバージョンによる差異はなし)。
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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_filter('woocommerce_thankyou_order_key', 'add_custom_thankyou_message', 10, 2);
function add_custom_thankyou_message($order_key, $order_id) {
return "Thank you for your purchase! Your order key is: " . $order_key;
}
このコードは、注文完了ページにカスタムメッセージを表示し、注文キーを含めます。引用元ページ: https://woocommerce.com/
サンプル2: 注文キーの変更
add_filter('woocommerce_thankyou_order_key', 'modify_order_key', 10, 2);
function modify_order_key($order_key, $order_id) {
return 'Custom-' . $order_key;
}
上記のコードは、元の注文キーに”Custom-“のプレフィックスを追加して返します。引用元ページ: https://woocommerce.com/
サンプル3: 一時的なリダイレクト
add_filter('woocommerce_thankyou_order_key', 'redirect_after_thankyou', 10, 2);
function redirect_after_thankyou($order_key, $order_id) {
wp_redirect('https://example.com/thank-you');
exit;
}
このサンプルでは、注文完了後に特定のURLにリダイレクトします。引用元ページ: https://woocommerce.com/
サンプル4: 特定条件下でのメッセージ表示
add_filter('woocommerce_thankyou_order_key', 'conditional_thankyou_message', 10, 2);
function conditional_thankyou_message($order_key, $order_id) {
if (get_post_meta($order_id, '_special_offer', true)) {
return "Congratulations! You have a special offer on this order. Your order key is: " . $order_key;
}
return $order_key;
}
このコードは、特別なオファーがある場合に、特定メッセージを追加します。引用元ページ: https://woocommerce.com/
サンプル5: 注文データの外部サービスへの送信
add_filter('woocommerce_thankyou_order_key', 'send_order_data_to_service', 10, 2);
function send_order_data_to_service($order_key, $order_id) {
$order = wc_get_order($order_id);
$data = array(
'order_key' => $order_key,
'total' => $order->get_total(),
// その他必要なデータ
);
wp_remote_post('https://external-service.com/api', array('body' => $data));
return $order_key;
}
このコードは、注文完了時に注文データを外部サービスに送信します。引用元ページ: https://woocommerce.com/