我使用Wordpress和WooCommerce插件创建了网站,我成功地让用户从前端发布产品,
我想显示产品订单
正如您在此图片中看到的那样
我成功展示了该产品的总销售额, 现在我想向所有买家展示产品/
的信息到目前为止我已经有了这段代码
<?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];
}
?>
此代码工作正常但它显示有关所有产品的详细信息
我想要的是:根据产品ID显示有关产品的信息
所以我想编辑此代码以获得取决于post->id
$p = $post->ID;
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
答案 0 :(得分:6)
好的,所以阅读您的问题并假设$ post-&gt; ID是您要显示包含订单的产品的ID,这就是您所需要的:
<?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;
}
}
答案 1 :(得分:1)
如果我理解你的问题,也许你可以尝试一下。但您必须确保$post
正在引用订单。
$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];
}
您可以在此处阅读WP_Query()函数的文档:http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters