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

概要

woocommerce_order_query フィルタは、WooCommerceの注文クエリに対してカスタマイズを行うために使用されるフックです。このフィルタを使うことで、特定の条件に基づいて注文の取得方法を変更したり、結果をフィルタリングしたりできます。主に以下のような機能を実装する際によく使われます。

  1. 特定のステータスの注文だけを取得する
  2. 注文の取得結果にカスタムメタデータを追加またはフィルタリング
  3. 管理画面での注文リスト表示をカスタマイズ
  4. 特定のユーザーによる注文のみを表示
  5. 日付範囲を指定して注文をフィルタリング
  6. 特定の商品のみを含む注文を取得

構文

add_filter( 'woocommerce_order_query', 'custom_function_name', 10, 2 );

パラメータ

  • $query: WP_Queryオブジェクト。またはそれに準ずるデータ。
  • $query_vars: クエリに使用される変数の配列。

戻り値

  • フィルタリングされたWP_Queryオブジェクト。

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

  • 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_order_query', 'filter_orders_by_status', 10, 2 );
function filter_orders_by_status( $query, $query_vars ) {
    $query->set( 'post_status', 'wc-completed' ); // 完了した注文のみ取得
    return $query;
}

このコードは、完了した注文(wc-completed)のみを取得するためにクエリをフィルタリングします。
引用元: https://woocommerce.com/

サンプルコード2: カスタムメタデータに基づくフィルタリング

add_filter( 'woocommerce_order_query', 'filter_orders_by_custom_meta', 10, 2 );
function filter_orders_by_custom_meta( $query, $query_vars ) {
    $meta_query = $query->get( 'meta_query' );
    $meta_query[] = array(
        'key'     => '_custom_meta_key',
        'value'   => 'custom_value',
        'compare' => '=',
    );
    $query->set( 'meta_query', $meta_query );
    return $query;
}

このコードは、特定のカスタムメタデータ(_custom_meta_key)が特定の値(custom_value)と一致する注文を取得します。
引用元: https://developer.wordpress.org/

サンプルコード3: ユーザーIDによるフィルタリング

add_filter( 'woocommerce_order_query', 'filter_orders_by_user_id', 10, 2 );
function filter_orders_by_user_id( $query, $query_vars ) {
    if ( is_admin() && current_user_can( 'manage_woocommerce' ) ) {
        $query->set( 'customer_id', get_current_user_id() ); // 現在のユーザーの注文のみ取得
    }
    return $query;
}

このコードは、管理画面において、現在のユーザーが管理者であれば、そのユーザーが行った注文だけを表示します。
引用元: https://woocommerce.com/

サンプルコード4: 日付範囲によるフィルタリング

add_filter( 'woocommerce_order_query', 'filter_orders_by_date_range', 10, 2 );
function filter_orders_by_date_range( $query, $query_vars ) {
    $query->set( 'date_query', array(
        array(
            'after'     => '1 month ago',
            'before'    => 'today',
            'inclusive' => true,
        ),
    ));
    return $query;
}

このコードは、過去1ヶ月以内の注文のみを取得するために日付クエリを設定します。
引用元: https://woocommerce.com/

サンプルコード5: 商品IDによるフィルタリング

add_filter( 'woocommerce_order_query', 'filter_orders_by_product_id', 10, 2 );
function filter_orders_by_product_id( $query, $query_vars ) {
    $query->set( 'meta_query', array(
        array(
            'key'   => '_order_items',
            'value' => 'specific-product-id',
            'compare' => 'LIKE'
        )
    ));
    return $query;
}

このコードは、特定の商品IDが含まれた注文を取得します。
引用元: https://developer.wordpress.org/

以上が woocommerce_order_query フィルタの使用例です。お好みに応じてクエリをカスタマイズしてください。

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


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