Reputation: 2860
i have created Website using Wordpress and WooCommerce Plugin, and i have success making users post products from frontend,
i want to display product orders
as you can see in this image
i success showing total sales for the product, now i want to show all buyers infomations for the product/
so far i have got this code
<?php
$p = $post->ID;
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
$customer_orders = $my_query->posts;
//print_r($customer_orders);
foreach ($customer_orders as $customer_order) {
$order = new WC_Order();
$order->populate($customer_order);
$orderdata = (array) $order;
$fields = array_values($orderdata);
//print_r($fields);
echo 'Status: '.$fields[1];
echo '<br>Date : '.$fields[2];
echo '<br>Email : '.$fields[16];
}
?>
This Code is Working Fine But it Show details about all products
What i Want is: to show informations about about product depending on product ID
so i want to edit this code to get results depending on post->id
$p = $post->ID;
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
Upvotes: 2
Views: 2273
Reputation: 5905
Ok, so reading your question and assuming $post->ID is the id of the product you want to display the orders containing, this is what you need:
<?php
$products = array();
foreach (get_posts('post_type=shop_order&numberposts=-1&post_status=publish') as $order) {
$order = new WC_Order($order->ID);
foreach($order->get_items('line_item') as $item) {
$product_id = (!empty($item['variation_id'])) ? $item['variation_id'] : $item['product_id'];
$products[] = $product_id;
}
if (in_array($post->ID,$products)) {
echo 'Status: '.$order->order_status;
echo '<br>Date : '.$order->order_date;
echo '<br>Email : '.$order->billing_email;
}
}
Upvotes: 6
Reputation: 4559
If I understood your question well, maybe you can try this. But you have to make sure that $post
is refering to the order.
$p = $post->ID;
$args = array(
'p' => $p,
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
if ( $my_query->have_posts() ) {
$my_query->next_post();
$customer_order = $my_query->post;
$order = new WC_Order();
$order->populate($customer_order);
$orderdata = (array) $order;
$fields = array_values($orderdata);
//print_r($fields);
echo 'Status: '.$fields[1];
echo '<br>Date : '.$fields[2];
echo '<br>Email : '.$fields[16];
}
You can read the doc of the WP_Query() function here : http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
Upvotes: 1