admin管理员组文章数量:1426338
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
本文标签: phpHow to get Woocommerce order product info
版权声明:本文标题:php - How to get Woocommerce order product info 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745381466a2656182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论