如何在woocommerce中获取订单ID列表

时间:2020-03-17 10:16:41

标签: php wordpress woocommerce

我有一张表,列出了订单中的合并物料,并想列出从中获取物料的订单号

<table class="widefat">


    <?php

    $ord_prods = $this->get_order_report_data(
      array(
        'data'         => $query_data,
        'order_by'     => 'order_item_qty DESC',
        'group_by'     => 'product_id',
        'query_type'   => 'get_results',
        'filter_range' => true,            
        'parent_order_status'   => false,
        'order_status' => array( 'processing' ),
      )
    );
    if ( $ord_prods ) {
      // @codingStandardsIgnoreStart
      echo '<tr>
      <th style="border: 1px solid #f00;"><b>Product Name</b></th>
      <th style="border: 1px solid #f00;"><b>Category</b></th>
      <th style="border: 1px solid #f00;"><b>Count ('.count($ord_prods).')</b></th>
      <th style="border: 1px solid #f00;"><b>Price</b></th>
      <th style="border: 1px solid #f00;"><b>Total</b></th>
      <tr>
      ';
      foreach ( $ord_prods as $product ) {


        $p = wc_get_product( $product->product_id ); 
        $product_cats = implode(', ',array_map(function($obj){ return $obj->name; }, wp_get_post_terms( $product->product_id, 'product_cat' )));
        echo '
        <tr >

          <td class="name" style="border: 1px solid #000;"><a href="' . esc_url( add_query_arg( 'product_ids', $product->product_id ) ) . '">' . esc_html( get_the_title( $product->product_id ) ) . '</a></td>
          <td style="border: 1px solid #000;"><span class="text-muted">'  . $product_cats . '</span></td>
          <td class="count" style="border: 1px solid #000;">' . esc_html( $product->order_item_qty ) . '</td>
          <td style="border: 1px solid #000;">'.esc_html($p->get_price()).'</td>
          <td class="name" style="border: 1px solid #000;">'.$p->get_price()* $product->order_item_qty.'</td>
                </tr>';
      }
      // @codingStandardsIgnoreEnd
    } else {
      echo '<tr><td colspan="3">' . esc_html__( 'No products found in range', 'woocommerce' ) . '</td></tr>';
    }
    ?>
  </table>

      <?php

    }
  }

上面是表的代码,该表按顺序列出了合并的商品以及类别,价格和数量,我如何获取所列产品的订单ID并在新表中显示它们

1 个答案:

答案 0 :(得分:1)

请尝试使用此代码,让我知道它是否适合您。我还没有测试过...

global $woocommerce, $product;
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);

$orders = $my_query->posts;
echo "</pre>";
print_r($orders);//this will get you all the orders from backend
echo "<pre>";
foreach ($orders as $order){

$order = new WC_Order( $order['id'] );//here get the orderid from $orders array
$items = $order->get_items(); // this will get all product details under orderid
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
}

}
相关问题