如何在woocommerce中获取当前用户的所有订单

Dam*_*man 8 php wordpress woocommerce

我想在插件函数中获取当前用户下的所有订单。

我正在使用这个:

    function get_all_orders(){
        $customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
            'numberposts' => $order_count,
            'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => wc_get_order_types( 'view-orders' ),
            'post_status' => array_keys( wc_get_order_statuses() )
        ) ) );
   return $customer_orders;
  }
Run Code Online (Sandbox Code Playgroud)

这在主题中运行良好,但在自定义插件中它不返回任何内容。难道我做错了什么?我应该先打电话给一些 WooCommerce 课程吗?

kon*_*tur 11

自从最初的问题以来,API 可能已经发生了变化,但这更加优雅,并且使用了 WC 自己的功能:

$args = array(
    'customer_id' => $user_id
);
$orders = wc_get_orders($args);
Run Code Online (Sandbox Code Playgroud)

您可以使用更多其他 $args