【问题标题】:Get list of woocommerce orders plugin获取 woocommerce 订单插件列表
【发布时间】:2016-03-23 08:19:51
【问题描述】:

我目前正在为 Woocommerce 开发一个插件。

现在,我有点卡住了,那就是如何获得所有订单。

这是我目前的代码:

    global $woocommerce;
    global $post;
    $order = new WC_Order(102249);
    $_order =   $order->get_items();
    foreach($_order as $order_product_detail){
        echo "<b>Product ID:</b> ".$order_product_detail['product_id']."<br>";
        echo "<b>Product Name:</b> ".$order_product_detail['name']."<br><br>";
}

这行得通。但我需要所有订单。现在我只收到订单号。 102249.我试过用
$order = new WC_Order($post-&gt;ID);
但这给了我一个通知:Notice: Trying to get property of non-object
我认为这与 WordPress 尚未加载 global $post 有关。
那么我怎样才能获得所有订单。我怎样才能等待 WordPress 完全加载?

我查看了 WordPress 和 Woocommerce 的 codex,不幸的是这对我没有帮助。

【问题讨论】:

  • 你到底想要什么
  • @NavnitMishra,所有 Woocommerce 订单...
  • 在 woocommerce 中,有两种方法可以获取所有订单,首先获取特定客户的所有订单或您数据库中可用的所有订单列表
  • @NavnitMishra,所以您没有获取所有订单的标准函数吗?因为我的代码针对一个指定的订单执行了我想要的操作。需要有可能循环使用所有订单 ID。因为我宁愿使用 Woocommerce 功能也不愿自己将其从数据库中取出。

标签: php wordpress plugins


【解决方案1】:
function wc_get_customer_orders() {

    // Get all customer orders
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',

        'post_type'   => wc_get_order_types(),
        'post_status' => array_keys( wc_get_order_statuses() ),
    ) );

    $customer = wp_get_current_user();



    print_r($customer_orders);



}
add_action( 'woocommerce_before_my_account', 'wc_get_customer_orders' );

试试这个

【讨论】:

  • 太棒了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
  • 2014-05-24
  • 2014-03-07
  • 2022-01-14
  • 2018-10-26
  • 1970-01-01
相关资源
最近更新 更多