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

概要

woocommerce_order_status_changed アクションは、WooCommerce で注文のステータスが変更された際にトリガーされるフックです。このアクションを使用すると、特定の条件下で自動的に処理を実行することが可能になります。例えば、注文が完了した時点での通知を送信したり、在庫情報を更新したりする処理を実装する際によく使われます。

よく使われる機能

  1. 注文完了時の顧客へのメール通知
  2. 在庫の自動更新
  3. クーポンコードの自動適用
  4. 外部システムへの注文データの送信
  5. 注文履歴のログ記録
  6. マーケティングツールとの連携

構文

do_action( 'woocommerce_order_status_changed', $order_id, $old_status, $new_status );

パラメータ

  • $order_id: 注文の ID
  • $old_status: 変更前のステータス
  • $new_status: 変更後のステータス

戻り値

このアクションは戻り値を返しません。フックを利用した複数の処理を実行することができます。

使用可能なバージョン

  • 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_order_status_changed', 'send_order_completion_email', 10, 3 );
function send_order_completion_email( $order_id, $old_status, $new_status ) {
    if ( 'completed' === $new_status ) {
        $order = wc_get_order( $order_id );
        $to = $order->get_billing_email();
        $subject = 'ご注文が完了しました';
        $message = 'ご注文ありがとうございます。';
        wp_mail( $to, $subject, $message );
    }
}

このコードは、注文が「完了」に変更されたときに顧客にメールを送信します。

サンプル2: 在庫の自動更新

add_action( 'woocommerce_order_status_changed', 'update_stock_on_order_complete', 10, 3 );
function update_stock_on_order_complete( $order_id, $old_status, $new_status ) {
    if ( 'completed' === $new_status ) {
        $order = wc_get_order( $order_id );
        foreach ( $order->get_items() as $item ) {
            $product = $item->get_product();
            $product->reduce_stock( $item->get_quantity() );
        }
    }
}

このコードは、注文が完了した際に、対応する商品の在庫を減少させる処理を実行します。

サンプル3: ログ記録

add_action( 'woocommerce_order_status_changed', 'log_order_status_change', 10, 3 );
function log_order_status_change( $order_id, $old_status, $new_status ) {
    $log_message = sprintf( 'Order ID %1$s status changed from %2$s to %3$s', $order_id, $old_status, $new_status );
    error_log( $log_message );
}

このコードは、注文のステータス変更の履歴をエラーログに記録します。

サンプル4: 外部APIとの連携

add_action( 'woocommerce_order_status_changed', 'notify_external_api', 10, 3 );
function notify_external_api( $order_id, $old_status, $new_status ) {
    if ( 'completed' === $new_status ) {
        $data = array( 'order_id' => $order_id, 'status' => $new_status );
        wp_remote_post( 'https://externalapi.com/notify', array( 'body' => $data ) );
    }
}

このコードは、注文が完了した際に外部APIに通知を送信します。

サンプル5: クーポンの自動適用

add_action( 'woocommerce_order_status_changed', 'apply_coupon_on_order_complete', 10, 3 );
function apply_coupon_on_order_complete( $order_id, $old_status, $new_status ) {
    if ( 'completed' === $new_status ) {
        $order = wc_get_order( $order_id );
        $order->apply_coupon( 'AUTOMATICCOUPON' ); // 自動適用するクーポンコード
        $order->calculate_totals();
    }
}

このコードは、注文が完了した際に自動的にクーポンを適用します。

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


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