获取wooccommerce订单插件列表


Get list of woocommerce orders plugin

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

现在,我有点陷入困境,这就是如何获得所有订单的方法。

这是我迄今为止的代码:

    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->ID);
但这给了我一个通知:Notice: Trying to get property of non-object
我想这是因为WordPress还没有加载global $post
那么我怎样才能得到所有订单呢。我该如何等待WordPress完全加载?

我看过WordPress和Wooccommerce的代码,不幸的是,这对我没有帮助。

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' );

试试这个