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

概要

woocommerce_payment_successful_resultは、WooCommerceの決済が成功した際に実行されるフックです。このフックを利用することで、決済完了後に特定のアクションを実行することができます。例えば、顧客への通知や、他のシステムとの連携、特別なトラッキングの実施などを行うことができます。よく使われる機能としては、以下のようなものがあります。

  1. 購入確認メールの送信
  2. カスタムリダイレクトの実施
  3. 受注データの外部システムへの送信
  4. 購入後のクーポンを発行
  5. 顧客へのサンクスページのカスタマイズ
  6. データベースへの追加情報の保存

構文

add_action('woocommerce_payment_successful_result', 'your_function_name', 10, 2);

パラメータ

  • $result (array): 決済結果を含む配列。
  • $order_id (int): 成功した注文のID。

戻り値

このアクション自体は戻り値を持ちません。

使用可能なWooCommerceのバージョン

WooCommerce 2.1.0以降。

使用可能なWordPressのバージョン

WordPress 4.0以降。

サンプルコード

サンプルコード1: 購入確認メールの送信

このコードは、決済が成功した後にカスタムメールを送信します。

add_action('woocommerce_payment_successful_result', 'send_custom_purchase_email', 10, 2);

function send_custom_purchase_email($result, $order_id) {
    $order = wc_get_order($order_id);
    $to = $order->get_billing_email();
    $subject = 'ご購入ありがとうございます';
    $message = 'ご購入いただきありがとうございます。';

    wp_mail($to, $subject, $message);
}

引用元: https://woocommerce.com/document/woocommerce-custom-emails/

サンプルコード2: カスタムリダイレクトの実施

このコードは、決済完了後に特定のページへリダイレクトします。

add_action('woocommerce_payment_successful_result', 'custom_redirect_after_payment', 10, 2);

function custom_redirect_after_payment($result, $order_id) {
    $redirect_url = 'https://example.com/thank-you';
    wp_redirect($redirect_url);
    exit;
}

引用元: https://wordpress.org/support/article/functions/

サンプルコード3: 外部システムへの注文データ送信

このコードは、決済が成功した際に注文データを外部APIへ送信します。

add_action('woocommerce_payment_successful_result', 'send_order_data_to_external_api', 10, 2);

function send_order_data_to_external_api($result, $order_id) {
    $order = wc_get_order($order_id);
    $data = array(
        'order_id' => $order_id,
        'total' => $order->get_total(),
        'currency' => $order->get_currency(),
    );

    $response = wp_remote_post('https://api.example.com/orders', array(
        'method'    => 'POST',
        'body'      => json_encode($data),
        'headers'   => array(
            'Content-Type' => 'application/json',
        ),
    ));
}

引用元: https://developer.wordpress.org/rest-api/reference/wp_remote_post/

サンプルコード4: 購入後のクーポン発行

このコードは、決済完了後に顧客にクーポンを発行します。

add_action('woocommerce_payment_successful_result', 'issue_coupon_after_payment', 10, 2);

function issue_coupon_after_payment($result, $order_id) {
    $coupon_code = 'BUYER2023';
    $order = wc_get_order($order_id);

    // クーポンを発行するロジックをここに追加(例: メタデータとして追加)
    $order->add_meta_data('issued_coupon', $coupon_code);
}

引用元: https://woocommerce.com/document/woocommerce-coupons/

サンプルコード5: タンキングページのカスタマイズ

このコードは、決済後のサンクスページをカスタマイズします。

add_action('woocommerce_payment_successful_result', 'custom_thank_you_page', 10, 2);

function custom_thank_you_page($result, $order_id) {
    echo '<h2>ありがとうござます!あなたの購入が完了しました。</h2>';
    // その他のカスタマイズをここに追加
}

引用元: https://woocommerce.com/document/woocommerce-thank-you-page/

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

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

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


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