Woocommerce报告'get_order_report_data'不起作用



我对以下查询的响应为null。我正在尝试获得产品小计的总和和产品的自定义成本字段。我可以在不使用where子句的情况下获得良好的数据,但使用where子句可以获得NULL。where子句用于获取包含自定义meta"dropshipper_name"和特定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' ),
            )
        );
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
    
  }
}

最新更新