Override functionalities in Woocommerce Reports section

I need to customize the Reports in WooCommerce, so I have to edit the core file class-wc-report-sales-by-date.php, such file doesn't use any hook.

Check this in WC source code: includes/admin/reports/class-wc-report-sales-by-date.php

I have to edit the 393 line, this variable: $this->report_data->total_sales I need to customize the Total Sales amount to add it another value.

How can I override WooCommerce this core file?

Upvotes: 1

Views: 1578

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254251

Never override core files… There is other ways made for that. If you look at line 411, you have woocommerce_admin_report_data filter hook to make changes, this way (example):

add_filter( 'woocommerce_admin_report_data', 'custom_admin_report_data', 10, 1 );
function custom_admin_report_data( $report_data ){
    // HERE you make your calculations and changes
    // New amout to set (example)
    $new_calculated_amount = 100;

    // Set the new amounts for "total_sales" key
    $report_data->total_sales = $new_calculated_amount;

    // Raw data output just for testing, to get the keys and the structure of the data
    // to be removed
    echo '<pre>'; print_r($report_data); echo '</pre>';

    // Return the changed data object
    return $report_data;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

I have included a line of code that outputs the raw data that you should remove… It's just to see the data structure and the changes made by the function on the "total_sales" value…


The Raw data output is something like this (that gives you the data structure to better make changes):

stdClass Object
(
    [order_counts] => Array
        (
            [0] => stdClass Object
                (
                    [count] => 1
                    [post_date] => 2017-11-21 16:45:43
                )
        )
    [coupons] => Array
        (
        )
    [order_items] => Array
        (
            [0] => stdClass Object
                (
                    [order_item_count] => 1
                    [post_date] => 2017-11-21 16:45:43
                )
        )
    [refunded_order_items] => 0
    [orders] => Array
        (
            [0] => stdClass Object
                (
                    [total_sales] => 48
                    [total_shipping] => 15
                    [total_tax] => 5
                    [total_shipping_tax] => 3
                    [post_date] => 2017-11-21 16:45:43
                )
        )
    [full_refunds] => Array
        (
        )
    [partial_refunds] => Array
        (
        )
    [refund_lines] => Array
        (
        )
    [total_tax_refunded] => 0
    [total_shipping_refunded] => 0
    [total_shipping_tax_refunded] => 0
    [total_refunds] => 0
    [total_tax] => 5.00
    [total_shipping] => 15.00
    [total_shipping_tax] => 3.00
    [total_sales] => 48.00
    [net_sales] => 25.00
    [average_sales] => 3.57
    [average_total_sales] => 6.86
    [total_coupons] => 0.00
    [total_refunded_orders] => 0
    [total_orders] => 1
    [total_items] => 1
)

So as you can see you also need to make changes on the "orders" object data as you also have the "total_sales" key…

Upvotes: 3

Related Questions