Wordpress how to override function get_order_report_data from wc_admin_report

1.9k views Asked by At

I want to change a part of the get_order_report_data() function inside class-wc-admin-report.php without touching the core.

I would like to change the order status filter, right now it is:

$query['where']  = "
            WHERE   posts.post_type     = 'shop_order'
            AND     posts.post_status   = 'publish'
            AND     tax.taxonomy        = 'shop_order_status'
            AND     term.slug           IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
            ";

And I want to change the term.slug to custom_status.

I don't now how to do this with a plugin or something like that

2

There are 2 answers

0
James Jones On BEST ANSWER

This question seems a bit muddled, but here is my best interpretation.

If you want to add a custom status to capture in the query write a filter function like this:

function woocommerce_reports_order_statuses_filter( $order_status ){
    $order_status[] = 'custom_status';
    return $order_status;
}
add_filter( 'woocommerce_reports_order_statuses', 'woocommerce_reports_order_statuses_filter' );

Later on in the function all the statuses in that array get 'wc-' appended to their values for the actual query string. So you'll need your status to have that prefix. Or... you can modify the querystring with a later filter:

function woocommerce_reports_get_order_report_query_filter( $query ){
    $custom_status = 'custom_status';
    $query['where'] = substr_replace( $query['where'], $custom_status , strpos( $query['where'], 'wc-' . $custom_status), strlen('wc-' . $custom_status)  );
    return $query;
}
add_filter( 'woocommerce_reports_get_order_report_query', 'woocommerce_reports_get_order_report_query_filter' );

All this filter code gets added to your theme functions.php or custom plugin code.

3
rnevius On

Filters are applied here, which will allow you to alter term.slug however you'd like.

In a custom plugin (or functions.php of your theme), add a filter:

function override_order_report_data_terms($slug_array) {
    return array('custom_status');
}
add_filter('woocommerce_reports_order_statuses', 'override_order_report_data_terms');

Read more about add_filter() in the Codex.