Запрашивать заказы WooCommerce, сгруппированные по нескольким статусам заказов


Как я вижу из документации WooCommerce, WC_Order_Query позволяет передавать в качестве параметра запроса только один статус заказа . Кто-нибудь нашел обходной путь, позволяющий запрашивать несколько статусов заказов в одном запросе?

Допустим, я хочу запросить заказы со статусом "выполнено" И "возвращено".

Как это можно сделать с помощью стандартного класса WC_Order_Query?

Пример из документа:

// Get orders on hold.
$args = array(
    'status' => 'on-hold',
);

$orders = wc_get_orders( $args );

Ссылка на wc_order_query() и WC_Order_Query WooCommerce - торговля документация

Author: LoicTheAztec, 2019-04-11

3 answers

Документация по wc_get_orders и WC_Order_Query является бедным… Теперь относительно статуса заказа в WC_Order_Query, вы можете передать массив статусов заказов:

// Display "completed" orders count
$statuses = ['completed'];
$orders = wc_get_orders( ['limit' => -1, 'status' => $statuses] );
echo '<p>' . sprintf( __('Count of "%s" orders: %s'), implode('", "', $statuses), count($orders) ) . '</p>';

// Display "refunded" orders count
$statuses = ['refunded'];
$orders = wc_get_orders( ['limit' => -1, 'status' => $statuses] );
echo '<p>' . sprintf( __('Count of "%s" orders: %s'), implode('", "', $statuses), count($orders) ) . '</p>';

// Display "completed" and "refunded"  orders count
$statuses = ['completed','refunded'];
$orders = wc_get_orders( ['limit' => -1, 'status' => $statuses] );
echo '<p>' . sprintf( __('Count of "%s" orders: %s'), implode('", "', $statuses), count($orders) ) . '</p>';

Протестировано и работает.

 6
Author: LoicTheAztec, 2019-04-11 20:47:31

Вместо передачи параметра "статус" вы можете использовать параметр "post_status" в виде массива, например:

// Get orders on hold and process
$args = array(
    'post_status' => array('wc-processing','on-hold')
);

$orders = wc_get_orders( $args );
 1
Author: Shehroz Ahmed, 2019-10-25 17:27:24

Мне потребовалось некоторое время, чтобы понять, что такое wordpress + woocommerce, но это работает идеально:)

if (!function_exists('custom_filter_for_shop_order')) {

    function custom_filter_for_shop_order($query)
    {
        global $typenow;

        if (
            is_admin() && $query->is_main_query() && in_array($typenow, wc_get_order_types('order-meta-boxes'), true)
            && isset($_GET['filter_status']) && is_array($_GET['filter_status'])
        ) {

            $query->set('post_status', $_GET['filter_status']);

            remove_filter('pre_get_posts', 'custom_filter_for_shop_order');
        }
    }
    add_filter('pre_get_posts', 'custom_filter_for_shop_order');
}
 0
Author: hasmai, 2020-11-09 22:42:00