按多个订单状态分组的WooCommerce订单查询

时间:2019-04-11 作者:Aleksandar Mitic

正如我从WooCommerce文档中看到的,WC_Order_Query 允许only one single order status 作为查询参数传递。是否有人找到一些解决方法,允许在同一查询中查询多个订单状态?

假设我想查询状态为“已完成”和“已退款”的订单。

如何通过标准WC_Order_Query

文档示例:

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

$orders = wc_get_orders( $args );
链接到wc_order_query() and WC_Order_Query WooCommerce文档

3 个回复
最合适的回答,由SO网友:LoicTheAztec 整理而成

有关的文档wc_get_orders and WC_Order_Query 很差…现在关于a中的订单状态WC_Order_Query, you can pass an array of order statuses:

// 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>\';
已测试并正常工作。

SO网友:Shehroz Ahmed

您可以将“post\\u status”参数用作如下数组,而不是传递“status”参数:

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

$orders = wc_get_orders( $args );

SO网友:hasmai

我花了一段时间才理解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\');
}

相关推荐

如何在PHP标记内使用带有do_Action函数的参数

我正在使用一个WordPress插件,它允许集成操作将其集成到一个主题中。使用GeneratePress的元素特性,我将动作添加为挂钩。目前我使用的代码是:<?php do_action(webcomic_integrate_landing_page); ?> 操作本身有一些布尔参数列在this website. 我最感兴趣的是webcomic_meta.如何将webcomic\\u元参数(设置为false)合并到PHP标记中以运行此操作?