最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

admin - WooCommerce: Add New Report Tab

programmeradmin0浏览0评论

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

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

Share Improve this question asked Oct 11, 2018 at 13:17 swaniswani 313 bronze badges 2
  • 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 Commented Oct 11, 2018 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 Commented Oct 11, 2018 at 13:25
Add a comment  | 

1 Answer 1

Reset to default 2
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;
}
发布评论

评论列表(0)

  1. 暂无评论