'Woocommerce Reports 'get_order_report_data' not working

I am getting null response on the below query. I am trying to get sum of the products subtotal and a custom cost field for products. I am able to get the data fine without the where clause but getting NULL with the where clause. The where clause is for getting the orders that have the custom meta 'dropshipper_name' in it with a specific meta_value.

$this->report_data->order_totals = (array) $this->get_order_report_data(
            array(
                    'data' => array(
                            '_cost_total'   => array(
                                    'type'              => 'order_item_meta',
                                    'order_item_type'   => 'line_item',
                                    'function'          => 'SUM',
                                    'name'              => '_alg_wc_cog_cost',
                            ),
                            '_profit_total' => array(
                                    'type'              => 'order_item_meta',
                                    'order_item_type'   => 'line_item',
                                    'function'          => 'SUM',
                                    'name'              => '_line_subtotal',
                            ),
                    ),
                    'where_meta'        => array(
                        array(
                            'meta_key'      => 'dropshipper_name',
                            'meta_value'    => 'alex',
                            'operator'      => '=',
                        ),
                    ),
                    'filter_range' => true,
                    'order_status' => array( 'completed', 'processing', 'on-hold', 'pending' ),
            )
        );


Solution 1:[1]

include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');

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,
    ) );
    //echo "<pre>";
    //print_r($sales_by_country_orders);
    ?>
    <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
    
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maulik patel