【问题标题】:Wordpress - Woocommerce - Retrieve orders for specific productWordpress - Woocommerce - 检索特定产品的订单
【发布时间】:2014-11-15 20:01:30
【问题描述】:

我正在使用 woocommerce wordpress 插件,并且我已经使用 WC_Order 类检索了所有订单的列表。 WC_Order documentation

这会返回有关订单的简单信息,例如 ID、日期和状态,但我需要知道客户购买的产品。

到目前为止,我的工作代码是:

$args = array(
    'post_type' => 'shop_order',
    'post_status' => 'publish',
    'meta_key' => '_customer_user',
    'posts_per_page' => '-1'
);

$my_query = new WP_Query($args);
$customer_orders = $my_query->posts;

$all_order_data = array();

foreach ($customer_orders as $customer_order) {
    $order = new WC_Order();
    $order->populate($customer_order);
    $orderdata = (array) $order;
    $all_order_data[] = $orderdata;
}

为了方便查看数据,我将其输出为 json:

echo json_encode($all_order_data);

但是,我需要能够查看每个订单购买了哪些产品。我找不到方法来做到这一点,我也没有在网上看到任何关于实现此功能的信息。同样,如果可能,检索附加到产品的订单对我来说也非常重要。

以前有人做过吗?或者谁能​​指出我正确的方向?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您可以使用订单对象上的get_items() 方法查看订单中的商品。在我的示例中,$all_order_data 将是查询订单中已购买的所有商品的数组。

    $customer_orders = new WP_Query($args);
    
    $all_order_data = array();
    
    while ($customer_orders->have_posts() ){
        $customer_orders->the_post();) {
        $order = new WC_Order();
        $all_order_data[] = $order->get_items();
    }
    
    wp_reset_postdata();
    

    【讨论】:

      【解决方案2】:

      @helgatheviking 走在正确的轨道上,但她的示例代码无法编译。你需要做的就是替换

      $orderdata = (array) $order;
      $all_order_data[] = $orderdata;
      

      $all_order_data[] = $order->get_items();
      

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-18
        • 2019-01-26
        • 1970-01-01
        相关资源
        最近更新 更多