php - woocommerce get_order_report_data to show order_item_id

58

I would like to show order_item_id by get_order_report_data()

I can use the array to show product id and order id :

array(
    '_product_id' => array(
        'type' => 'order_item_meta',
        'order_item_type' => 'line_item',
        'function' => '',
        'name' => 'product_id'
    ),
    'order_id' => array(
    'type' => 'order_item',
    'order_item_type' => 'line_item',
    'function' => '',
    'name' => 'order_id'
    )
)

But not for order item id.

Any suggestion? Deeply thx.

791

Answer

Solution:

You can achieve your desired result by using this code.

<?php   
    $sold_products = $wc_report->get_order_report_data(array(
    'data' => array(
        '_product_id' => array(
            'type' => 'order_item_meta',
            'order_item_type' => 'line_item',
            'function' => '',
            'name' => 'product_id'
        ),

        '_qty' => array(
            'type' => 'order_item_meta',
            'order_item_type' => 'line_item',
            'function' => 'SUM',
            'name' => 'quantity'
        ),
        '_line_subtotal' => array(
            'type' => 'order_item_meta',
            'order_item_type' => 'line_item',
            'function' => 'SUM',
            'name' => 'gross'
        ),
        '_line_total' => array(
            'type' => 'order_item_meta',
            'order_item_type' => 'line_item',
            'function' => 'SUM',
            'name' => 'gross_after_discount'
        )
    ),
    'query_type' => 'get_results',
    'group_by' => 'product_id',
    'where_meta' => '',
    'order_by' => 'quantity DESC',
    'order_types' => wc_get_order_types('order_count'),
    'filter_range' => TRUE,
    'order_status' => array('completed'),
));
848

Answer

Solution:

add_filter( 'woocommerce_admin_reports', 'my_custom_woocommerce_admin_reports', 10, 1 );
function my_custom_woocommerce_admin_reports( $reports ) {
    $sales_by_ebay_payment = array(
        'sales_by_ebay_payment' => array(
            'title'         => 'Sales By Ebay Payment',
            'description'   => '',
            'hide_title'    => true,
            'callback'      => 'sales_by_ebay_payment_callback',
        ),
    );

    // This can be: orders, customers, stock or taxes, based on where we want to insert our new reports page
    $reports['orders']['reports'] = array_merge( $reports['orders']['reports'], $sales_by_ebay_payment);

    return $reports;
}
function sales_by_ebay_payment_callback() {
    $report = new WC_Report_Sales_By_Ebay_Payment();
    $report->output_report();
}
class WC_Report_Sales_By_Ebay_Payment extends WC_Admin_Report {
  
  /**
   * Output the report.
   */
  public function output_report() {
    $ranges = array(
      'year'         => __( 'Year', 'woocommerce' ),
      'last_month'   => __( 'Last month', 'woocommerce' ),
      'month'        => __( 'This month', 'woocommerce' ),
    );

    $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'month';

    if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', '7day' ) ) ) {
      $current_range = 'month';
    }

    $this->check_current_range_nonce( $current_range );
    $this->calculate_current_range( $current_range );

    $hide_sidebar = true;

    include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' );
  }
  
  /**
   * Get the main chart.
   */
  public function get_main_chart() {
    global $wpdb;
    $where_meta = array();
    $query_data = array(
      'ID' => array(
          'type'     => 'post_data',
          'function' => 'COUNT',
          'name'     => 'total_orders',
          'distinct' => true,
      ),
      '_payment_method' => array(
          'type'      => 'meta',
          'function'  => '',
          'name'      => 'Ebay'
      ),
      '_order_total'   => array(
          'type'      => 'meta',
          'function'  => 'SUM',
          'name'      => 'order_total'
      ),
    );
    $where_meta[] = array(
        'meta_key' => '_payment_method', 
        'meta_value' =>'ebay_managed_payment', 
        'operator' => '=', 
        'type' => 'meta'
        );
    $sales_by_country_orders = $this->get_order_report_data( array(
      'data'                  => $query_data,
      'query_type'            => 'get_results',
      'group_by'              => 'Ebay',
      'where_meta'            => $where_meta,
      'filter_range'          => true,
      'order_types'           => wc_get_order_types( 'sales-reports' ),
      'order_status'          => array( 'completed' ),
      'parent_order_status'   => false,
    ) );
    ?>
    <table class="widefat">
      <thead>
          <tr>
              <th><strong>Payment Method</strong></th>
              <th><strong>Number Of Orders</strong></th>
              <th><strong>Sales</strong></th>
          </tr>
      </thead>
      <tbody>
          <?php foreach( $sales_by_country_orders as $order ) { 
          ?>
          <tr>
              <td><?php echo $order->Ebay; ?></td>
              <td><?php echo $order->total_orders; ?></td>
              <td><?php echo wc_price($order->order_total); ?></td>
          </tr>
          <?php } ?>
      </tbody>
    </table>
    <?php
    
  }
}

People are also looking for solutions to the problem: php - Mysql build query with multiple constraints

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.