7

I am trying to get all woocommerce orders and find info about the products in each order. I have seen many examples on here and elsewhere on the web and they all seem to say to do the same thing but it isn't working. I am on Wordpress 4.1.1 and Woocommerce 2.3.3 here is my code:

$filters = array(
    'post_status' => 'any',
    'post_type' => 'shop_order',
    'posts_per_page' => 200,
    'paged' => 1,
    'orderby' =>'modified',
    'order' => 'ASC'
);

$loop = new WP_Query( $filters );

while ( $loop->have_posts() ) {
    $loop->the_post();
    $order = new WC_Order($loop->post->ID);

    foreach ($order->get_items() as $key => $lineItem) {
        print_r($lineItem);
    }
}

The problem is when I print_r($lineItem) there are only three properties only two of which are ever populated. A typical print_r($lineItem) looks like this:

Array ( 
    [name] => Fouta Towel – Pearl Grey & White Stripe 
    [type] => line_item 
    [item_meta] => 
)

How do I get the rest of the information about this order item, e.g the product id, whether it is a single / variable product etc

6

Have tried your code and it works fine and infact it also gives out the details of each product in the orders. The code which i tried

$filters = array(
    'post_status' => 'any',
    'post_type' => 'shop_order',
    'posts_per_page' => 200,
    'paged' => 1,
    'orderby' => 'modified',
    'order' => 'ASC'
);

$loop = new WP_Query($filters);

while ($loop->have_posts()) {
    $loop->the_post();
    $order = new WC_Order($loop->post->ID);

    foreach ($order->get_items() as $key => $lineItem) {

        //uncomment the following to see the full data
        //        echo '<pre>';
        //        print_r($lineItem);
        //        echo '</pre>';
        echo '<br>' . 'Product Name : ' . $lineItem['name'] . '<br>';
        echo 'Product ID : ' . $lineItem['product_id'] . '<br>';
        if ($lineItem['variation_id']) {
            echo 'Product Type : Variable Product' . '<br>';
        } else {
            echo 'Product Type : Simple Product' . '<br>';
        }
    }
}

And the output i got from the same.

enter image description here

Try this and let me know how it works for you

  • it works but it creates problem when i try to use it inside the class ..and when i access it it throws me Uncaught Exception: Invalid order – Latheesh V M Villa Oct 16 '18 at 19:58
  • also $loop->the_post(); is it required? – Latheesh V M Villa Oct 16 '18 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.