【问题标题】:How to get all orders of current user in woocommerce如何在woocommerce中获取当前用户的所有订单
【发布时间】:2017-03-07 07:59:22
【问题描述】:

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

我正在使用这个:

    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;
  }

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

【问题讨论】:

  • 我已经在一个插件中测试了你的代码,激活它,并且在前端没有任何需求就可以完美地工作。你的问题出在别的地方……

标签: php wordpress woocommerce


【解决方案1】:

API 可能与最初的问题相比有所改变,但这更加优雅,并且使用了 WC 自己的函数:

$args = array(
    'customer_id' => $user_id
);
$orders = wc_get_orders($args);

您可以使用many more other $args

【讨论】:

    【解决方案2】:
        if (!class_exists('WooCommerce')) :
            require ABSPATH . 'wp-content/plugins/woocommerce/woocommerce.php';
            $orders = get_all_orders();
        endif;
    
        function get_all_orders() {
            $customer_orders = get_posts(apply_filters('woocommerce_my_account_my_orders_query', array(
                'numberposts' => -1,
                '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;
        }
    

    试试这个代码。

    【讨论】:

      猜你喜欢
      • 2020-05-08
      • 2017-07-02
      • 2017-10-11
      • 2022-01-08
      • 2015-01-27
      • 2016-12-21
      • 2015-09-07
      • 2016-05-10
      • 2022-01-14
      相关资源
      最近更新 更多