Scott L
Scott L

Reputation: 85

Filtering orders list in WooCommerce with HPOS

I'm in the process of converting an open source plugin to be HPOS-compatible. One of its features is additional filters on the orders list in admin (inline with the order statuses), eg: example

I can't seem to actually execute the modification of the query. Previously, I was hooking into pre_get_posts which obviously is no longer relevant when WP_Post is no longer being used.

Then the same for a filter field, for which I was hooking into restrict_manage_posts. found the equivalent hook: woocommerce_order_list_table_restrict_manage_orders

Anyone have an idea which hook to use?

Upvotes: 6

Views: 2987

Answers (3)

LoicTheAztec
LoicTheAztec

Reputation: 253609

As WC_Order_Query is used to query orders for HPOS, the following filters should still work:

  • woocommerce_order_query_args filter hook (replaces pre_get_posts),
  • woocommerce_order_query filter hook (replaces pre_get_posts),
  • and may be try woocommerce_order_data_store_cpt_get_orders_query filter hook.

For restrict_manage_posts hook now you will use:
woocommerce_order_list_table_restrict_manage_orders

Some others replacements hooks with HPOS:
  • To edit existing columns or add custom columns to admin Orders list:

    • manage_woocommerce_page_wc-orders_columns replaces the hook
      manage_edit-shop_order_columns,
    • manage_woocommerce_page_wc-orders_custom_column replaces the hook
      manage_shop_order_posts_custom_column.
  • To make custom columns sortable in admin Order list use:

    • woocommerce_shop_order_list_table_sortable_columns replacing the hook
      manage_edit-shop_order_sortable_columns
  • To make custom order metadata searchable in admin Order list use:

    • woocommerce_order_table_search_query_meta_keys replacing the hook
      woocommerce_shop_order_search_fields.
  • To edit the filters and navigation in admin Orders list:

    • woocommerce_order_list_table_extra_tablenav replaces the hook manage_posts_extra_tablenav.
  • For Bulk actions in admin Orders list:

    • bulk_actions-woocommerce_page_wc-orders replaces the hook
      bulk_actions-edit-shop_order
    • handle_bulk_actions-woocommerce_page_wc-orders has 2 arguments $column and $order and replaces the hook handle_bulk_actions-edit-shop_order

On High-Performance Order Storage documentation, there is a link to High Performance Order Storage Upgrade Recipe Book where you will find the path(s) to related core files you are looking for.

In this documentation, you have:

use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;

that points to woocommerce/src/Internal/DataStores/Orders, where OrdersTableQuery.php file is the right location to look at.

Inside the method maybe_override_query() you have at line 233:

$pre_query = apply_filters( 'woocommerce_hpos_pre_query', null, $this, $this->sql );

Inside the method build_query() you have at line 877:

$clauses = (array) apply_filters_ref_array( 'woocommerce_orders_table_query_clauses', array( $pieces, &$this, $this->args ) );

And so on…

Upvotes: 10

Davide Iandoli
Davide Iandoli

Reputation: 137

If your filter should run only in orders list, consider using this hook to replace pre_get_posts:

woocommerce_shop_order_list_table_prepare_items_query_args

because it affects only queries in the list table, while woocommerce_order_query_args would have impact on all query orders on the site.

Here below a sample code:

add_filter('woocommerce_shop_order_list_table_prepare_items_query_args', 'my_custom_query_args');

function my_custom_query_args( $query_args ) {
$meta_query = array(
            array(
                'key'   => '_my_meta',
                'value' => 'my_value',
            )
        );

        $query_args['meta_query'] = array_merge(
            $query_args['meta_query'] ?? array(),
            $meta_query
        );

        return $query_args;
}

Upvotes: 0

morepurplemorebetter
morepurplemorebetter

Reputation: 321

I originally commented with a question, but deleted it, because I was understanding the strike-through incorrectly. I though you wrote that woocommerce_order_list_table_restrict_manage_orders is the replacement hook for pre_get_posts.

For others with similar low reading comprehension, the updated hooks for the admin order page are:

OLD NEW
pre_get_posts woocommerce_order_query_args
restrict_manage_posts woocommerce_order_list_table_restrict_manage_orders
manage_edit-shop_order_columns manage_woocommerce_page_wc-orders_columns
manage_shop_order_posts_custom_column manage_woocommerce_page_wc-orders_custom_column
bulk_actions-edit-shop_order bulk_actions-woocommerce_page_wc-orders
handle_bulk_actions-edit-shop_order handle_bulk_actions-woocommerce_page_wc-orders

I'v also included some others that I had a hard time locating, mostly because get_current_screen()->id has changed for the admin order table.

Upvotes: 13

Related Questions