Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress how to override function get_order_report_data from wc_admin_report

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

like image 905
FamousWolluf Avatar asked Jun 09 '15 08:06

FamousWolluf


2 Answers

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.

like image 96
James Jones Avatar answered Oct 12 '22 22:10

James Jones


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.

like image 23
rnevius Avatar answered Oct 12 '22 23:10

rnevius