我使用 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'
);
最佳答案
好的,所以阅读您的问题并假设 $post->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;
}
}
关于php - 前端的 woocommerce 买家电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20787587/