プラグインWooCommerceのwoocommerce_payment_complete_order_status_$THIS->GET_STATUSフィルタの使用方法・解説

概要

woocommerce_payment_complete_order_status_$THIS->GET_STATUSフィルタは、WooCommerceで支払いが完了した際の注文のステータスを変更するために使用されます。このフィルタは、特定の注文ステータスに基づいて異なる動作を実行したい場合に非常に便利です。このフィルタの利用方法には、以下のようなシナリオが考えられます。

  1. 特定の支払いゲートウェイに依存したステータスの変更
  2. 定期購入商品のステータス管理
  3. クーポンの使用に基づく条件付きステータス変更
  4. 外部システムとの統合に際するステータスの調整
  5. 支払いエラーによる特別なステータスの設定
  6. 顧客の忠誠度プログラムに基づく特別なステータス付与

構文

add_filter( 'woocommerce_payment_complete_order_status_{status}', 'your_function_name', 10, 2 );

パラメータ

  • {status}: 特定の支払いゲートウェイや条件に基づいた状態
  • your_function_name: フィルタを適用する関数の名前
  • 第一引数: 注文のID
  • 第二引数: 現在のステータス

戻り値

  • 変更したい注文の新しいステータス

WooCommerce および WordPressのバージョン

  • WooCommerce: 4.0以降
  • WordPress: 5.0以降

サンプルコード

サンプル1: 特定のゲートウェイでのステータス変更

add_filter( 'woocommerce_payment_complete_order_status_paypal', 'change_order_status_for_paypal', 10, 2 );
function change_order_status_for_paypal( $status, $order_id ) {
    return 'completed'; // PayPalの場合は「完了」ステータスにする
}

このコードは、PayPalで支払いが完了した際に自動的に注文のステータスを「完了」に変更します。

サンプル2: 定期購入商品に特化したステータス変更

add_filter( 'woocommerce_payment_complete_order_status_subscription', 'change_subscription_order_status', 10, 2 );
function change_subscription_order_status( $status, $order_id ) {
    // 定期購入の場合、特定のステータスに変更
    return 'processing';
}

このコードは、定期購入が完了した際に注文のステータスを「処理中」に変更します。

サンプル3: クーポン使用時の条件付きステータス変更

add_filter( 'woocommerce_payment_complete_order_status', 'conditional_coupon_status_change', 10, 2 );
function conditional_coupon_status_change( $status, $order_id ) {
    $order = wc_get_order( $order_id );
    if ( $order->get_used_coupons() ) {
        return 'on-hold'; // クーポンが使用された場合は「保留」ステータスにする
    }
    return $status;
}

このコードは、注文でクーポンが使用された場合、注文のステータスを「保留」に変更します。

サンプル4: 特定のエラーに対するステータス変更

add_filter( 'woocommerce_payment_complete_order_status', 'error_based_status_change', 10, 2 );
function error_based_status_change( $status, $order_id ) {
    $order = wc_get_order( $order_id );
    if ( $order->has_status( 'failed' ) ) {
        return 'failed'; // 支払いエラー時は「失敗」ステータスを維持
    }
    return $status;
}

このコードは、支払いが失敗した際に注文のステータスを「失敗」として保持します。

サンプル5: 顧客のプログラムに基づくステータス変更

add_filter( 'woocommerce_payment_complete_order_status', 'loyalty_status_change', 10, 2 );
function loyalty_status_change( $status, $order_id ) {
    $customer_id = get_post_field( 'post_author', $order_id );
    if ( is_customer_loyal( $customer_id ) ) {
        return 'completed'; // 顧客がロイヤルティプログラムに加入している場合は「完了」
    }
    return $status;
}

このコードは、顧客がロイヤルティプログラムに参加している場合、注文のステータスを「完了」に変更します。

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

アクション 使用可能性
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

この表は、特定のアクションにおけるwoocommerce_payment_complete_order_status_$THIS->GET_STATUSフィルタの使用可能性を示しています。

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


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