melnic
melnic

Reputation: 11

Customizing Woocommerce reports time interval

I need to insert a last4Days option in the reports tab. I've done that by changing the original files but WooCommerce keeps changing to the original after a while. So I tried to find a filter to do so, but I'm not finding it.

Upvotes: 0

Views: 663

Answers (1)

Josh LaBau
Josh LaBau

Reputation: 620

Unfortunately the Woo team did not do the user-friendly thing and include a filter on the report-length arguments. Short of some meta-programming magic (like dynamically over-writing the arguments in the .php file's text before load, very dangerous) the only way I can see is to go up the instantiation ladder to the point where the report is generated, and then have it call a custom class based on the one you are you using. Luckily WooCommerce does provide a filter for the path to the report file, it's in wc-class-admin-reports.php and the hook is called wc_admin_reports_path. If we were extending the coupon usage report, it would look a little something like:

add_filter('wc_admin_reports_path', 'redirect_coupon_report_class_path', 10, 2);
function redirect_coupon_report_class_path($path, $report_name, $class) {
  if($report_name == 'coupon-usage') {
    $path = 'path/to/my/custom/class.php';
  }
  return $path;
}

What you'll need to do is copy the class of the report you're trying to alter, and paste it somewhere in your theme or plugin. Then, add a filter calling a function that will check to see if the report being passed in is the one you want to modify, and redirect it to your custom class. Within your custom class, you can do pretty much whatever you want, but as with any override you'll want to see what they change in that file with each update. You can extend the class like so:

class WC_Report_Coupon_Usage_Custom extends WC_Report_Coupon_Usage {

   // Call the parent constructor
   function __construct() {
     parent::__construct();
   }

   // Add a method that you would have previously overwritten directly in the plugin file
   public function output_report() {

        $ranges = array(
            'year'         => __( 'Year', 'woocommerce' ),
            'last_month'   => __( 'Last Month', 'woocommerce' ),
            'month'        => __( 'This Month', 'woocommerce' ),
            '7day'         => __( 'Last 7 Days', 'woocommerce' ),
            '4day'         => __( 'Last 4 Days', 'woocommerce' )
        );

        $this->chart_colours = array(
            'discount_amount' => '#3498db',
            'coupon_count'    => '#d4d9dc',
        );

        $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';

        if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) {
            $current_range = '7day';
        }

        $this->calculate_current_range( $current_range );

        include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php');
    }

}

Upvotes: 1

Related Questions