Sj03rs
Sj03rs

Reputation: 937

Get list of woocommerce orders plugin

I am working on a plugin at the moment for Woocommerce.

Right now, I am a little stuck on something, and that is how to get all orders.

This is my code so far:

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

This works. But I need all orders. Now I only get order no. 102249. I have tried to use
$order = new WC_Order($post->ID);
But this gives me a notice: Notice: Trying to get property of non-object
I assume this has to do that WordPress has not loaded the global $post yet.
So how can I get all orders. And how can I wait for WordPress to have fully loaded?

I have looked at the codex of WordPress and Woocommerce, this did not help me unfortunately.

Upvotes: 0

Views: 1780

Answers (1)

Navnit Mishra
Navnit Mishra

Reputation: 505

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

try this

Upvotes: 1

Related Questions