php – Woocommerce query get_order_report_data by searching value within an array-ThrowExceptions

Exception or error:

I’m trying to set up a custom Woocommerce report that will report on the number of orders that have a shipment status within the report timeframe. The problem is that the shipment meta is an array. Ideally, I would like to query for any order that has a _wc_shipment_tracking_items.date_shipped > $report_start_date. Or at the very least just query where the _wc_shipment_tracking_items is available in the Order which would mean there is a tracking record. Then I could possibly loop through all the orders and look at the details in the meta.

The meta looks like the following…

        » 0
        » » tracking_provider   ups
        » » custom_tracking_provider
        » » custom_tracking_link
        » » tracking_number 1ZX5266903451XXXXX
        » » date_shipped    1583798400
        » » tracking_id 6b70334d5708b78e917aXXXXXXXX

I’ve tried to set up the query data array like this, thinking that by including _wc_shipment_tracking_items, that the query would only return if that meta_key was in the record.

$query_data = array(
  'ID' => array(
      'type'     => 'post_data',
      'name'     => 'order',
      'distinct' => true,
  '_order_total'   => array(
      'type'      => 'meta',
      'function'  => 'SUM',
      'name'      => 'order_total'
    '_wc_shipment_tracking_items'   => array(
      'type'      => 'meta',
      'function'  => '',
      'name'      => 'shipment_tracking'


And run the query this way:

$sales_by_shipment_status = $this->get_order_report_data( array(
  'data'                  => $query_data,
  'query_type'            => 'get_results',
  'filter_range'          => true,
  'order_types'           => wc_get_order_types( 'sales-reports' ),
  'order_status'          => array( 'completed', 'shipped-invoiced'),
  'parent_order_status'   => false,
) );

But it just returns all the records like normal. Any thoughts? Thanks!

How to solve:

Leave a Reply

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