概要
woocommerce_after_pay_action
はWooCommerceプラグイン内で使用されるフックの一つです。このアクションは、顧客が支払いを完了した後に発生します。具体的には、支払いの処理が成功した後に追加の処理を実行したい場合に非常に便利です。主に以下のような機能を実装する際に使用されます。
- 支払い後のカスタム確認メッセージの表示
- 支払い情報の送信やログ記録
- カスタムメール通知の送信
- サードパーティAPIとの連携
- データベースへの支払い履歴の保存
- ユーザーのポイントや報酬プログラムの更新
構文
add_action('woocommerce_after_pay_action', 'your_custom_function', 10, 1);
パラメータ
$order
: 注文オブジェクト。顧客の支払いに関連する情報を含む。
戻り値
このアクションは直接的な戻り値を持ちませんが、関数内で行う処理の結果に影響を与えることができます。
使用可能なバージョン
- WooCommerceのバージョン: 3.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_after_pay_action', 'log_payment_details');
function log_payment_details($order) {
$log_file = 'payment_log.txt';
$payment_data = sprintf("Order ID: %s, Amount: %s, Status: %sn",
$order->get_id(),
$order->get_total(),
$order->get_status());
file_put_contents($log_file, $payment_data, FILE_APPEND);
}
このコードは、支払いが完了した際に注文の詳細を指定したログファイルに記録します。
サンプルコード2
add_action('woocommerce_after_pay_action', 'send_custom_email');
function send_custom_email($order) {
$to = 'admin@example.com';
$subject = '新しい注文';
$message = '新しい注文が処理されました。注文ID: ' . $order->get_id();
wp_mail($to, $subject, $message);
}
このコードは、支払い完了後に管理者に新しい注文の通知メールを送信します。
サンプルコード3
add_action('woocommerce_after_pay_action', 'update_loyalty_points');
function update_loyalty_points($order) {
$user_id = $order->get_user_id();
// ユーザーのポイントを更新するロジックを実装
// 例えば add_points_to_user($user_id, $order->get_total());
}
このコードは、支払いが完了した後にユーザーのロイヤリティポイントを更新するための関数を呼び出します。
サンプルコード4
add_action('woocommerce_after_pay_action', 'redirect_after_payment');
function redirect_after_payment($order) {
wp_redirect('https://example.com/thank-you');
exit();
}
このコードは、支払いが完了した後に指定されたURL(お礼のページ)にリダイレクトします。
サンプルコード5
add_action('woocommerce_after_pay_action', 'custom_order_status_action');
function custom_order_status_action($order) {
if ($order->get_status() === 'completed') {
// カスタムアクションを実行
}
}
このコードは、支払いが完了した際に特定のカスタムアクションを実行する条件を設定します。