php – How to get list of order id in woocommerce-ThrowExceptions

Exception or error:

I am having a table that lists consolidated items from the orders, and would like to list the order numbers from which the items are getting

<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

    }
  }

The above is the code for table that lists consolidated items in order along with category, price and quantity, How could i get the order id of the listed products and shown them in a new table

How to solve:

Please try this code and let me know if it works for you. I haven’t tested it yet…

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'];
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *