概要
woocommerce_default_order_status
フィルタは、WooCommerceにおいて新たに作成される注文のデフォルトのステータスを変更する際に使用されます。このフィルタを使用することで、サイト運営者は、注文処理がどのように行われるかをカスタマイズできます。
一般的な使用例:
- 新規注文の初期ステータスを「保留中」に設定する。
- 特定のユーザーグループに対して異なるデフォルトの注文ステータスを設定する。
- ショッピングカートのリセット後にデフォルトステータスを変更する。
- オーダーの特定の条件に基づきデフォルトのステータスを動的に変更する。
- アプリケーションのロジックに基づく複雑なフロー管理を行う。
- プラグインやテーマの仕様に応じた特定のデフォルトオーダーステータスの割り当て。
構文
add_filter( 'woocommerce_default_order_status', 'custom_default_order_status' );
function custom_default_order_status( $status ) {
// 追加のロジック
return $status;
}
パラメータ
$status
(string): デフォルトのオーダーステータス。通常は ‘pending’, ‘completed’ などの値が入ります。
戻り値
- (string): 新しく指定されたデフォルトのオーダーステータスを返す。
WooCommerceバージョン
- 利用可能:WooCommerce 2.0以上
WordPressバージョン
- 利用可能: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_default_order_status', 'set_default_order_status_to_pending' );
function set_default_order_status_to_pending( $status ) {
return 'pending';
}
このコードは、新たに作成される注文のデフォルトステータスを「保留中」に設定します。
サンプルコード2: 特定のユーザーに応じたステータス設定
add_filter( 'woocommerce_default_order_status', 'vary_default_order_status' );
function vary_default_order_status( $status ) {
if ( current_user_can( 'wholesale_customer' ) ) {
return 'completed';
}
return $status;
}
このコードでは、特定のロール(この例では「卸売顧客」)を持つユーザーの注文を「完了」に設定します。
サンプルコード3: 特定の商品を持つカートにデフォルトステータスを割り当てる
add_filter( 'woocommerce_default_order_status', 'default_order_status_for_special_product' );
function default_order_status_for_special_product( $status ) {
if ( isset( $_SESSION['cart'] ) && in_array( 'special_product_id', $_SESSION['cart'] ) ) {
return 'on-hold';
}
return $status;
}
このコードは、特定の商品がカートに含まれる場合、デフォルトのオーダーステータスを「保留中」に設定します。
サンプルコード4: オーダーステータスを動的に変更
add_filter( 'woocommerce_default_order_status', 'dynamic_order_status' );
function dynamic_order_status( $status ) {
$hour = date('G');
return ( $hour < 12 ) ? 'pending' : 'processing';
}
時間に基づいてデフォルトのオーダーステータスをた動的に変更します。午前中は「保留中」、午後は「処理中」となります。
サンプルコード5: ステータス変更のカスタムロジック
add_filter( 'woocommerce_default_order_status', 'custom_logic_default_status' );
function custom_logic_default_status( $status ) {
if ( is_user_logged_in() && get_user_meta( get_current_user_id(), 'special_status', true ) ) {
return 'completed';
}
return $status;
}
ユーザーがログインしていて、特定のメタデータを持っている場合、デフォルトのオーダーステータスを「完了」とします。これによって特定のユーザーにカスタマイズしたロジックを提供しています。