Skip to content

Instantly share code, notes, and snippets.

@kartikparmar
Last active July 24, 2019 17:35
Show Gist options
  • Save kartikparmar/1b43335ba7b481e78fffa536742a44f2 to your computer and use it in GitHub Desktop.
Save kartikparmar/1b43335ba7b481e78fffa536742a44f2 to your computer and use it in GitHub Desktop.
hide_wc_category_from_shop
<?php
/**
* Show products only of selected category.
*/
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
$hide_category = array( 126 ); // Ids of the category you don't want to display on the shop page
// if a product category and on the shop page
if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->term_id, $hide_category ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
@swina
Copy link

swina commented Nov 13, 2018

Hi the above is correct only for the shop page. If you are in the product page the categories will not be hidden. You will also loose the original order of the categories defined in the category setting page.
Your code could be improved like following:

function get_subcategory_terms( $terms, $taxonomies, $args ) {
        $new_terms 	= array();
        $hide_category 	= array( 126 ); // Ids of the category you don't want to display on the shop page
        //use an array to sort categories as per original order
        $sorted_menu = array();
     	// if a product category and on the shop page and even in the product page (class_exists("Woocommerce"))
    	if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && (is_shop() || class_exists('Woocommerce'))  ) {
    	    foreach ( $terms as $key => $term ) {
                 
                 if ( ! in_array( $term->term_id, $hide_category ) ) {
                      $new_terms[] = $term;
                      //add order key to array to sort
                      $sorted_menu[$key] = get_term_meta($term->term_id,'order',true);
    		 }
    	    }
            //resort new array with original order
            array_multisort( $sorted_menu, SORT_ASC, $new_terms );
    	    $terms = $new_terms;
    	}
     return $terms;
}

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

Also if you want to remove the category page of the hidden category you have to add the following (otherwise url http://yourdomain.com/category/category_slug will be still visible):

function check_category_is_disabled(){
      //check if is the category page
      $hide_category 	= array( 126 ); // Ids of the category you don't want to display on the shop page
      if ( is_product_category() ){
  	    global $wp_query;
  	    $cat = $wp_query->get_queried_object();
            //check if current category is disabled
            if ( ! in_array( $cat->term_id, $hide_category ) ) { 
                echo '<style>.page-title { display:none; }</style>';
                $wp_query->set_404();
                status_header( 404 );
                get_template_part( 404 );
                exit();
            }
      }
}

add_action( 'woocommerce_archive_description',  'check_category_is_disabled' ,10,1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment