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

概要

woocommerce_webhook_deliver_asyncは、WooCommerceのウェブフックが非同期的にデリバリーされる際に発火するフックです。主に以下のような機能の実装に使用されます。

  1. リアルタイムでのデータの送信
  2. 商品の在庫管理や変更通知
  3. 注文ステータスの更新通知
  4. 外部サービスとの連携(CRM、メールマーケティングなど)
  5. イベントトリガーによる自動処理
  6. カスタム通知の送信

構文

add_action('woocommerce_webhook_deliver_async', 'your_custom_function', 10, 4);

パラメータ

  • $webhook: ウェブフックオブジェクト。
  • $payload: 送信されるデータのペイロード。
  • $order: 注文オブジェクト(関連する場合)。
  • $request: HTTP リクエストオブジェクト(関連する場合)。

戻り値

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

バージョン要件

  • 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_webhook_deliver_async', 'log_webhook_delivery', 10, 4);

function log_webhook_delivery($webhook, $payload, $order, $request) {
    $log_entry = sprintf("Webhook %s delivered with payload: %s", $webhook->get_id(), json_encode($payload));
    error_log($log_entry);
}

このサンプルコードは、ウェブフックが配信されるたびに、その詳細をエラーログに記録します。

サンプル2: ウェブフックのカスタムプロセッサー

add_action('woocommerce_webhook_deliver_async', 'custom_process_webhook', 10, 4);

function custom_process_webhook($webhook, $payload, $order, $request) {
    // データを特定の外部APIに送信
    $url = 'https://api.example.com/webhook';
    wp_remote_post($url, array(
        'body' => json_encode($payload),
        'headers' => array('Content-Type' => 'application/json'),
    ));
}

このコードは、ウェブフックが送信されると、指定された外部APIにペイロードを送信します。

サンプル3: メール通知を送信

add_action('woocommerce_webhook_deliver_async', 'send_webhook_notification', 10, 4);

function send_webhook_notification($webhook, $payload, $order, $request) {
    $to = 'admin@example.com';
    $subject = 'Webhook Delivered';
    $message = 'A webhook has been delivered with the following payload: ' . print_r($payload, true);

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

このコードは、ウェブフックが配信されるたびに管理者にメール通知を送信します。

サンプル4: ウェブフック配信の後処理

add_action('woocommerce_webhook_deliver_async', 'post_delivery_actions', 10, 4);

function post_delivery_actions($webhook, $payload, $order, $request) {
    // 特定のカスタムアクションを実行
    if ($payload['status'] === 'completed') {
        update_post_meta($order->get_id(), '_status_notified', true);
    }
}

このサンプルは、ウェブフックが配信された後、注文が完了した場合にメタデータを更新します。

サンプル5: ウェブフックのエラー処理

add_action('woocommerce_webhook_deliver_async', 'handle_webhook_errors', 10, 4);

function handle_webhook_errors($webhook, $payload, $order, $request) {
    $response = wp_remote_request($webhook->get_delivery_url(), array('body' => $payload));

    if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
        // エラーログに記録
        error_log('Webhook delivery failed for ' . $webhook->get_id());
    }
}

このコードは、ウェブフックの配信が失敗した場合にエラーログに記録します。

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


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