Sometimes solutions are very simple, and you don’t need rocket science to fix your issue! A client of mine needed to hide a category from the Product Categories Shortcode ([product_categories]). A rare issue, but once again a quick fix! Here’s how you do it.

1. Find WooCommerce Category IDs

First of all you need a list of all the WooCommerce category IDs in your store. To do so you can proceed in two ways.

The first is purely manual. This is good if you have no more than 20 categories. Go to the Product Categories page, hover on each category and note down the ID as shown in the image.

Find WooCommerce category IDs

The second method is a little smarter 🙂 You can use PHP in the admin to print a list of category IDs, so you don’t need to worry about doing that manually.

You can use this snippet to print a list of all Category IDs:

/**
 * @snippet       Print List of Category IDs @ Product Categories Admin
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_action( 'product_cat_pre_add_form', 'bbloomer_list_all_product_cat_ids', 5 );
 
function bbloomer_list_all_product_cat_ids() {
 
$ids = '';
 
$categories = get_categories( array(               
   'taxonomy' => 'product_cat' ) );
  
foreach( $categories as $category ) {
    $ids .= $category->term_id . ', ';
} 
 
echo 'Category IDs: ' . $ids;
}

Here’s the result:

Get a list of Product Category IDs

2. Exclude the Category IDs from the “product_categories” Shortcode

Now that you have all IDs and the ID of the category you want to hide, simply exclude that from the list of ids inside the shortcode!

// -----------------------
// #1 Show all categories
 
[product_categories]
 
// -----------------------
// #2 Show all categories but ID = 8
// ## All category ids: 8, 15, 34, 20, 18, 37, 10
// ## See, this last version is missing the "8"!
 
[product_categories ids="15, 34, 20, 18, 37, 10"]
Share:

Leave a Reply