Snippet

How to Hide Selected Reports in WooCommerce Analytics

How to hide reports in woocommerce analyticsRemove specific analytics reports in wordpressHide analytics data from wordpress adminDisable certain analytics features in woocommerceCustomize analytics view in wordpressHide analytics menu items in wordpressRemove average sales from woocommerce analyticsDisable analytics section in wordpressHide analytics charts in woocommerceRestrict analytics access in wordpress

Explanation

If you're looking to tidy up your WooCommerce Analytics section by hiding certain reports or even the entire Analytics menu, this code snippet is just what you need.

Here's what it does:

  • Hides Specific Reports: The code removes specific reports like Revenue, Orders, Products, and more from the WooCommerce Analytics section. This is done by listing the report slugs you want to hide and then removing them from the menu.
  • Removes the Analytics Menu: If you want to hide the entire Analytics menu from the WordPress admin dashboard, this snippet will do that too.
  • Restricts Access: It ensures that only administrators can access the Analytics section. Non-admin users will be redirected away if they try to access it.

By using this code, you can customize the WooCommerce Analytics view to show only what you need, keeping your dashboard clean and focused.

Code

1<?php 2// Hook into the WooCommerce Admin initialization 3add_action('woocommerce_admin_init', 'wp_dudecom_customize_woocommerce_analytics'); 4 5function wp_dudecom_customize_woocommerce_analytics() { 6 // Check if the current user has the capability to manage WooCommerce 7 if (!current_user_can('manage_woocommerce')) { 8 return; 9 } 10 11 // Remove specific reports from WooCommerce Analytics 12 add_filter('woocommerce_analytics_report_menu_items', 'wp_dudecom_remove_analytics_reports', 10, 1); 13} 14 15function wp_dudecom_remove_analytics_reports($reports) { 16 // List of report slugs to remove 17 $reports_to_remove = array( 18 'revenue', // Remove Revenue report 19 'orders', // Remove Orders report 20 'products', // Remove Products report 21 'categories', // Remove Categories report 22 'coupons', // Remove Coupons report 23 'taxes', // Remove Taxes report 24 'downloads', // Remove Downloads report 25 'stock', // Remove Stock report 26 'customers', // Remove Customers report 27 ); 28 29 // Loop through the reports and unset the ones we want to remove 30 foreach ($reports_to_remove as $report_slug) { 31 if (isset($reports[$report_slug])) { 32 unset($reports[$report_slug]); 33 } 34 } 35 36 return $reports; 37} 38 39// Hook into the WooCommerce Admin menu 40add_action('admin_menu', 'wp_dudecom_remove_analytics_menu_items', 99); 41 42function wp_dudecom_remove_analytics_menu_items() { 43 // Remove the Analytics menu item completely 44 remove_menu_page('wc-admin&path=/analytics'); 45} 46 47// Restrict access to WooCommerce Analytics for non-admin users 48add_action('admin_init', 'wp_dudecom_restrict_analytics_access'); 49 50function wp_dudecom_restrict_analytics_access() { 51 // Check if the current user is not an administrator 52 if (!current_user_can('administrator')) { 53 // Redirect non-admin users away from the Analytics page 54 if (is_admin() && isset($_GET['page']) && $_GET['page'] === 'wc-admin&path=/analytics') { 55 wp_redirect(admin_url()); 56 exit; 57 } 58 } 59} 60?>

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.
  • You must have administrator access to implement these changes.

Implementation Steps:

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor if you are adding the code to functions.php, or go to Plugins > Editor if you are adding it to a custom plugin.
  3. Locate and open the functions.php file of your active theme or the custom plugin file where you want to add the code.
  4. Copy the provided code snippet and paste it at the end of the file.
  5. Save the changes to the file.
  6. Clear your browser cache and refresh your WordPress admin dashboard to see the changes take effect.

By following these steps, you will successfully hide selected reports in the WooCommerce Analytics section and restrict access to non-admin users.

If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert WordPress support.