概要
wc_order_statuses
フィルタは、WooCommerceプラグインで使われるフックで、注文のステータスを登録または変更する際に使用されます。これにより、デフォルトの注文ステータスを拡張したり、カスタムステータスを追加したりすることができます。このフィルタは、注文処理のカスタマイズや管理、レポート機能の強化など、様々な機能を実装する際に非常に役立ちます。よく使われる機能としては以下のものがあります:
- カスタム注文ステータスの追加
- 既存の注文ステータスのラベルの変更
- ステータスの表示順序の変更
- ステータスに関するメタデータの追加
- 管理画面での表示設定のカスタマイズ
- ステータスに基づく通知機能の追加
構文
add_filter('wc_order_statuses', 'custom_wc_order_statuses');
パラメータ
$order_statuses
: 現在の注文ステータスの配列。
戻り値
- カスタマイズされた注文ステータスの配列。
使用可能なバージョン
- WooCommerce: バージョン 2.1 以降
- WordPress: バージョン 3.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('wc_order_statuses', 'add_custom_order_status');
function add_custom_order_status($order_statuses) {
$order_statuses['wc-custom-status'] = _x('Custom Status', 'Order status', 'text-domain');
return $order_statuses;
}
このサンプルコードは、WooCommerceで「Custom Status」という名前のカスタム注文ステータスを追加します。これにより、管理画面の注文一覧などで新しいステータスを扱うことができるようになります。
(引用元: https://woocommerce.com/document/custom-order-statuses/)
サンプルコード2: 既存のステータスラベルの変更
add_filter('wc_order_statuses', 'change_order_status_label');
function change_order_status_label($order_statuses) {
if (isset($order_statuses['wc-completed'])) {
$order_statuses['wc-completed'] = _x('Order Completed - Thank You!', 'Order status', 'text-domain');
}
return $order_statuses;
}
このコードは、既存の「完了」ステータスのラベルを「Order Completed – Thank You!」に変更します。顧客へのメッセージ性を強化します。
(引用元: https://developer.woocommerce.com/document/modify-order-statuses/)
サンプルコード3: ステータスの表示順序の変更
add_filter('wc_order_statuses', 'reorder_wc_order_statuses');
function reorder_wc_order_statuses($order_statuses) {
$status = $order_statuses['wc-pending'];
unset($order_statuses['wc-pending']);
$order_statuses = array_merge(array('wc-pending' => $status), $order_statuses);
return $order_statuses;
}
このサンプルコードは、デフォルトの表示順序を変更し、「保留中」ステータスをリストの最初に移動します。これにより、運用上の優先順位を調整できます。
(引用元: https://docs.woocommerce.com/document/custom-order-statuses/)
サンプルコード4: ステータスに関連するメタデータの追加
add_filter('wc_order_statuses', 'add_meta_data_to_order_status');
function add_meta_data_to_order_status($order_statuses) {
$order_statuses['wc-custom-status']['meta_data'] = 'オーダーに関する特別なインフォメーション';
return $order_statuses;
}
このサンプルコードは、「Custom Status」ステータスに関連するメタデータを追加します。特定の情報を表示する必要がある場合に役立ちます。
(引用元: https://developer.woocommerce.com/document/tutorial-custom-order-statuses/)
サンプルコード5: ステータスに基づく通知機能の追加
add_filter('wc_order_statuses', 'add_notification_order_status');
function add_notification_order_status($order_statuses) {
$order_statuses['wc-notification-status'] = _x('Notification Needed', 'Order status', 'text-domain');
return $order_statuses;
}
このコードは、「Notification Needed」というステータスを追加し、そのステータスの下にある注文に対して特定の通知機能をトリガーできるようにします。
(引用元: https://woocommerce.com/document/woocommerce-notes/)
以上がwc_order_statuses
フィルタに関する詳細な解説とサンプルコードです。これにより、WooCommerceでのカスタム注文ステータスの実装が理解できるでしょう。