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

概要

woocommerce_order_is_paid_statuses フィルタは、WooCommerceにおいて受注が「支払い済み」と見なされるステータスをカスタマイズするために使用されます。このフィルタは主に、特定の支払い方法や条件に応じて支払い済みステータスの管理を行う際に利用されます。また、このフィルタを使用することで、以下のような機能を実装できます。

  1. 異なる支払い方法に基づくステータスの変更
  2. カスタム支払いゲートウェイの追加
  3. 注文処理時の状態管理の向上
  4. サブスクリプション商品における支払い状態の設定
  5. 特定のユーザーに対する支払いステータスのカスタマイズ
  6. テスト環境における注文ステータスの簡単な変更

構文

add_filter('woocommerce_order_is_paid_statuses', 'custom_paid_statuses');

function custom_paid_statuses($statuses) {
    // カスタムを追加
    $statuses[] = 'custom-status';
    return $statuses;
}

パラメータ

  • $statuses: すでに定義されている支払い済みステータスの配列。

戻り値

  • カスタマイズされた支払い済みステータスの配列。

使用可能なプラグインとバージョン

  • WooCommerce バージョン: 3.0 以上
  • WordPress バージョン: 4.4 以上

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

アクション 使用例
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_is_paid_statuses', 'add_custom_pay_status');

function add_custom_pay_status($statuses) {
    $statuses[] = 'on-hold'; // 'on-hold' ステータスを支払い済み状態に追加
    return $statuses;
}

このコードは、on-hold ステータスを支払い済みとして追加します。この場合、特定の条件(顧客や支払い方法など)のもとで、注文が保留中であっても支払い済みと見なされます。

引用元: https://woocommerce.com/

サンプルコード 2

add_filter('woocommerce_order_is_paid_statuses', 'customize_order_paid_status');

function customize_order_paid_status($statuses) {
    if (is_admin()) {
        $statuses[] = 'completed'; // 管理画面では 'completed' ステータスを追加
    }
    return $statuses;
}

このサンプルは、WooCommerce管理画面において completed ステータスを支払い済みとして追加します。これにより、管理者が注文を管理する際に、より柔軟なオプションが提供されます。

引用元: https://developer.woocommerce.com/

サンプルコード 3

add_filter('woocommerce_order_is_paid_statuses', 'filter_paid_status_for_specific_product');

function filter_paid_status_for_specific_product($statuses) {
    if (has_product_in_cart('123')) { // 商品ID 123 がカートにあるか確認
        $statuses[] = 'pending'; // その場合は 'pending' を支払い済みとして追加
    }
    return $statuses;
}

このコードは、特定の商品がカートにある場合、pending ステータスを支払い済みの状態として追加します。このような条件付き処理が可能になります。

引用元: https://woocommerce.com/

サンプルコード 4

add_filter('woocommerce_order_is_paid_statuses', 'override_payment_status_for_free_orders');

function override_payment_status_for_free_orders($statuses) {
    if (is_free_order()) { // 注文が無料の場合に確認
        $statuses[] = 'processing'; // 無料の注文を支払い済み状態として処理
    }
    return $statuses;
}

このサンプルは、無料の注文に対して processing ステータスを加え、ユーザーに明確な状況を提供するためのものです。

引用元: https://wordpress.org/

サンプルコード 5

add_filter('woocommerce_order_is_paid_statuses', 'extend_paid_statuses_for_subscriptions');

function extend_paid_statuses_for_subscriptions($statuses) {
    // サブスクリプション用の支払い済みステータスを追加
    $statuses[] = 'active';
    return $statuses;
}

このコードは、WooCommerceサブスクリプションに関連する active ステータスを支払い済みとして追加します。サブスクリプションの状態管理を向上させるために役立ちます。

引用元: https://woocommerce.com/

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


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