概要
woocommerce_reports_order_statuses
フィルタは、WooCommerceのレポート機能で使用される注文ステータスをカスタマイズするために使用されます。このフィルタを使用することで、標準の注文ステータスに加えて、独自のカスタムステータスをレポートに追加したり、特定の状態を除外したりすることができます。
このフィルタは以下のような機能を実装する際によく使われます。
– カスタム注文ステータスの追加
– 特定の注文ステータスの無効化
– レポート表示をカスタマイズ
– 分析用の特定のデータのみを抽出
– アドバンスドフィルタリング機能の実装
– データエクスポート時のカスタマイズ
構文
add_filter('woocommerce_reports_order_statuses', 'your_function_name');
パラメータ
$order_statuses
(array): フィルタされる注文ステータスの配列。
戻り値
- array: フィルタ後の注文ステータスの配列。
使用可能なプラグイン(WooCommerceのバージョン)
- 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_filter('woocommerce_reports_order_statuses', 'add_custom_order_status');
function add_custom_order_status($order_statuses) {
$order_statuses['wc-processing'] = '処理中';
return $order_statuses;
}
引用元: https://developer.wordpress.org/reference/hooks/woocommerce_reports_order_statuses/
2. 特定ステータスの削除
このサンプルは、特定の注文ステータスをレポートから除外します。
add_filter('woocommerce_reports_order_statuses', 'remove_completed_status');
function remove_completed_status($order_statuses) {
unset($order_statuses['wc-completed']);
return $order_statuses;
}
引用元: https://developer.wordpress.org/reference/hooks/woocommerce_reports_order_statuses/
3. 複数ステータスの変更
このサンプルコードは、複数の既存のステータス名を変更します。
add_filter('woocommerce_reports_order_statuses', 'change_order_status_names');
function change_order_status_names($order_statuses) {
$order_statuses['wc-on-hold'] = '保留中';
$order_statuses['wc-cancelled'] = 'キャンセル済み';
return $order_statuses;
}
引用元: https://developer.wordpress.org/reference/hooks/woocommerce_reports_order_statuses/
4. 追加のカスタムステータス
このコードは、レポートに「出荷済み」というカスタムステータスを追加します。
add_filter('woocommerce_reports_order_statuses', 'add_shipped_order_status');
function add_shipped_order_status($order_statuses) {
$order_statuses['wc-shipped'] = '出荷済み';
return $order_statuses;
}
引用元: https://developer.wordpress.org/reference/hooks/woocommerce_reports_order_statuses/
5. ステータスの配列に情報を追加
このサンプルでは、元のステータス配列にカスタムオプションを追加します。
add_filter('woocommerce_reports_order_statuses', 'add_custom_options_to_statuses');
function add_custom_options_to_statuses($order_statuses) {
$order_statuses['wc-pending'] = '保留中 - お客様の支払い待ち';
return $order_statuses;
}
引用元: https://developer.wordpress.org/reference/hooks/woocommerce_reports_order_statuses/