プラグインWooCommerceのwoocommerce_order_fully_refunded_statusフィルタの使用方法・解説

概要

woocommerce_order_fully_refunded_statusフィルタは、WooCommerceにおいてオーダーが完全に返金されたステータスをフィルタリングするためのフックです。このフィルタを使用することで、特定の条件に基づいて返金ステータスを変更したり、デフォルトの動作をカスタマイズしたりすることが可能です。

このフィルタは、以下のような機能を実装する際によく使われます。

  1. 返金された注文のカスタムステータスを設定する。
  2. 特定のユーザーやプロセスによる返金に対するロジックを追加する。
  3. 返金の通知やメッセージをカスタマイズする。
  4. 外部のシステムとの連携を行う際の条件を設定する。
  5. 売上データの集計や解析に必要なカスタムロジックを追加する。
  6. ショップのポリシーに基づいたカスタマイズされた返金プロセスを実装する。

構文

add_filter( 'woocommerce_order_fully_refunded_status', 'your_custom_function' );

パラメータ

  • $status: (string)現在の返金ステータス。デフォルトは refunded
  • $order: (WC_Order)対象の注文オブジェクト。

戻り値

  • (string)修正された返金ステータス。

利用可能なバージョン

  • WooCommerce: 3.0.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_filter( 'woocommerce_order_fully_refunded_status', 'custom_fully_refunded_status' );

function custom_fully_refunded_status( $status ) {
    return 'completed'; // ステータスを 'completed' に変更
}

このサンプルコードは、完全に返金されたオーダーのステータスを completed に設定します。

引用元: WooCommerce Documentation

サンプルコード2: 特定のユーザーの返金ステータスを制御

add_filter( 'woocommerce_order_fully_refunded_status', 'control_refund_status_for_user' );

function control_refund_status_for_user( $status, $order ) {
    if ( $order->get_user_id() === 5 ) {
        return 'refunded_special'; // 特定のユーザーに対してカスタムステータスを設定
    }
    return $status;
}

このコードは、ユーザーIDが5の注文に対して、カスタム返金ステータス refunded_special を設定します。

引用元: WooCommerce Documentation

サンプルコード3: 返金時に特定のメッセージを追加

add_filter( 'woocommerce_order_fully_refunded_status', 'add_refund_message' );

function add_refund_message( $status, $order ) {
    if ( $order->get_total() > 1000 ) {
        // ログや通知用に特別なメッセージを追加
        error_log( 'Large order refunded: ' . $order->get_id() );
    }
    return $status;
}

このコードは、1,000ドル以上の返金が行われた場合にログに記入するメッセージを追加します。

引用元: WooCommerce Documentation

サンプルコード4: フロントエンドでの返金ステータスを表示

add_filter( 'woocommerce_order_fully_refunded_status', 'display_refund_status_in_admin' );

function display_refund_status_in_admin( $status, $order ) {
    if ( is_admin() ) {
        // 管理画面での返金ステータスを変更
        return 'Refund processed by admin';
    }
    return $status;
}

このサンプルは、管理画面での返金ステータス表示をカスタマイズします。

引用元: WooCommerce Documentation

サンプルコード5: 返金時に他の条件を確認

add_filter( 'woocommerce_order_fully_refunded_status', 'conditional_refund_status' );

function conditional_refund_status( $status, $order ) {
    if ( $order->has_downloadable_item() && $order->get_total() < 50 ) {
        return 'refunded_with_conditions'; // 特定の条件が満たされた場合にステータスを変更
    }
    return $status;
}

このコードは、ダウンロードアイテムを含み、合計が50ドル未満の注文に対してカスタムステータスを適用します。

引用元: WooCommerce Documentation

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


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