2

I have a project for a school. The seats in the class are sold as variable WooCommerce products and student info is captured at checkout. I want to put a new tab in the Reports section of WooCommerce to store all of the rosters. I don't want to just put a new report on an existing tab. I have been searching around for how to do this for a full day.

Here is where I've gotten on my own. No idea if this is right or not.

When I dug into WC I found a filter called "woocommerce_admin_reports" that allows you to modify the values of an array called $reports. The filter is in this file:

wp-content\plugins\woocommerce\includes\admin\class-wc-admin-reports.php

This seemed like the right place to start, but one of the values is the callback which seems to use a get_report() to pull the correct file. When I look at get_report() it seems to be building a string and then pulling the report from wp-content\plugins\woocommerce\includes\admin\reports.

public static function get_report( $name ) {
    $name  = sanitize_title( str_replace( '_', '-', $name ) );
    $class = 'WC_Report_' . str_replace( '-', '_', $name );

    include_once apply_filters( 'wc_admin_reports_path', 'reports/class-wc-report-' . $name . '.php', $name, $class );

    if ( ! class_exists( $class ) ) {
        return;
    }

    $report = new $class();
    $report->output_report();
}

So, I simply copy/pasted a report and renamed it to:

wp-content\plugins\woocommerce\includes\admin\reports\class-wc-report-rosters.php

But all I get is a list of errors from all over the place:

Notice: Undefined variable: reports in reports in C:\xampp\htdocs\eele\wp-content\themes\eele-child\functions.php on line 1271

Notice: Undefined offset: 0 in C:\xampp\htdocs\eele\wp-includes\plugin.php on line 897

Notice: Undefined offset: 0 in C:\xampp\htdocs\eele\wp-includes\plugin.php on line 915

Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in C:\xampp\htdocs\eele\wp-includes\class-wp-hook.php on line 286

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\eele\wp-content\plugins\woocommerce\includes\admin\class-wc-admin-reports.php on line 146

Warning: array_keys() expects parameter 1 to be array, null given in C:\xampp\htdocs\eele\wp-content\plugins\woocommerce\includes\admin\class-wc-admin-reports.php on line 31

Warning: array_keys() expects parameter 1 to be array, null given in C:\xampp\htdocs\eele\wp-content\plugins\woocommerce\includes\admin\class-wc-admin-reports.php on line 33

Warning: current() expects parameter 1 to be array, null given in C:\xampp\htdocs\eele\wp-content\plugins\woocommerce\includes\admin\class-wc-admin-reports.php on line 33

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\eele\wp-content\plugins\woocommerce\includes\admin\views\html-admin-page-reports.php on line 14

Here is the function I put in functions.php to try to get this to work. Commenting out the add_filter() line removes all of the errors so I know all of the errors are from this effort.

function eele_add_wc_reports( $reports ){

    $reports['rosters'] = array(
        'title'   => __( 'Rosters', 'woocommerce' ),
        'reports' => array(
            'rosters' => array(
                'title'       => __( 'Rosters', 'woocommerce' ),
                'description' => '',
                'hide_title'  => true,
                'callback'    => array( __CLASS__, 'get_report' ),
            ),
        ),
    );
    return $reports;
}
add_filter( 'woocommerce_admin_reports', $reports );

Anyway, I know this is long. If you made it this far, thanks for taking the time. If there is a tutorial or something out there I have missed I'm more than happy to take a look. All of my searching has been made harder by the fact that lots of people want to know how to add a tab on the front end so I get lots of results for that kind of thing.

Please let me know if you have any questions.

Thanks,

Swani

  • Oh for crying out loud. I was re-reading my post again to make triple sure I hadn't missed anything and noticed my add_filter was messed up. It should be this: add_filter( 'woocommerce_admin_reports', 'eele_add_wc_reports' ); Once I did that, the tab appeared. I still haven't sorted out how to populate the report itself but I'm back on track. I'll leave this post up in case anyone else has a hard time figuring out how to do this. – swani Oct 11 '18 at 13:23
  • You can edit your post. it should be add_filter( 'woocommerce_admin_reports', 'eele_add_wc_reports' ); I guess. Let us know is that works. – Pim Oct 11 '18 at 13:25
1
add_filter('woocommerce_admin_reports','custom_tab');

function custom_tab($reports)
{
    $reports['custom_tab']= array(      
        'title'=>__('Custom Reports','woocommerce'),    
        'description'=> 'WooCommerce Orders Listing Here...',
        'hide_title'=> true,     
        'callback'=>array('wc_admin_orders_customers', 'display_orders_list_cusotomers')
    );
    return $reports;
}
  • 1
    You should always give some explanations with your answer code, editing your question. – LoicTheAztec Sep 5 at 8:57
  • Welcome to WPSE. When answering a question, a good quality answer includes an explanation of why this is the answer. – butlerblog Sep 5 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.