woocommerce买家在前端发送电子邮件

You*_*ehi 2 php wordpress woocommerce

我使用WordpressWooCommerce插件创建了网站,我成功地让用户从前端发布产品,
我想显示产品订单

正如你在这张图片中看到的那样
详细的图像

我成功显示该产品的总销售额,现在我想向所有买家展示该产品的信息/

到目前为止,我有这个代码

<?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];                           

 }

?>
Run Code Online (Sandbox Code Playgroud)

此代码工作正常但它显示有关所有产品的详细信息

我想要的是:根据产品ID显示有关产品的信息

所以我想编辑此代码以获得结果取决于 post->id

$p = $post->ID;
     $args = array(
       'post_type' => 'shop_order',
       'post_status' => 'publish',
       'meta_key' => '_customer_user',
       'posts_per_page' => '-1'
     );
Run Code Online (Sandbox Code Playgroud)

Lia*_*ley 6

好的,所以阅读你的问题并假设$ 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; 
    }   
}
Run Code Online (Sandbox Code Playgroud)