majeedraza

WooCommerce - Disable WooCommerce default category

Mar 11th, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Disable WooCommerce default category
  4. Plugin URI: https://www.willhosts.com/
  5. Description: Disables the default category behaviour for WooCommerce v3.3.1 onwards
  6. Version: 1.0.0
  7. Author: WillHosts Ltd
  8. Author URI: https://www.willhosts.com/
  9. */
  10.  
  11. /**
  12.  * @author     WillHosts Ltd
  13.  * @since      v1.0.0
  14.  * @copyright  Copyright (c) 2018 and Onwards, WillHosts Ltd. All rights reserved.
  15.  * @license    GPL
  16.  * By viewing, using, or actively developing this application in any way, you are
  17.  * henceforth bound the license agreement, and all of its changes, set forth by
  18.  * WillHosts Ltd.
  19.  */
  20.  
  21. // Cache default product cat option for WP_Term_Query
  22. $default_product_cat = get_option('default_product_cat');
  23.  
  24. // Filter WP_Term_Query to exclude default category
  25. add_action('pre_get_terms', function ($term_query) use ($default_product_cat) {
  26.     if (in_array('product_cat', $term_query->query_vars['taxonomy'])) {
  27.         $term_query->query_vars['exclude'][] = $default_product_cat;
  28.     }
  29. });
  30.  
  31. // Filter default product category option in case WooCommerce add a user option
  32. add_filter('pre_option_default_product_cat', function () {
  33.     return -1;
  34. });
  35.  
  36. // Hide 'uncategorised' instruction from CMS
  37. add_action('admin_head', function () {
  38.     echo(
  39.     <<<HTML
  40.     <style type="text/css">
  41.     .edit-tags-php.taxonomy-product_cat .edit-term-notes {
  42.         display:none;
  43.     }
  44. </style>
  45. HTML
  46.     );
  47. });
  48.  
  49. // Trigger 404 for product category on frontend
  50. add_action('get_header', function () use ($default_product_cat) {
  51.     if (is_tag($default_product_cat)) {
  52.         status_header(404);
  53.     }
  54. });
Add Comment
Please, Sign In to add comment