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
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
Share Improve this question edited Aug 25, 2016 at 9:38 dotZak 681 gold badge1 silver badge8 bronze badges asked Mar 3, 2015 at 22:14 tuscan88tuscan88 1731 gold badge1 silver badge6 bronze badges1 Answer
Reset to default 7Have 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.
Try this and let me know how it works for you