/woocommerce-hide-empty-categores.php
Forked from cklosowski/woocommerce-hide-empty-categores.php
Last active Sep 12, 2017
In addition to Chris, I added functionality that child categories will only expand if they are in the selected category.
<?php | |
/** | |
Plugin Name: WooCommerce - Exclude empty categories from widget | |
Plugin URI: https://chrisk.io/woocommerce-hide-categories-with-no-visible-products-in-the-product-category-widget/ | |
Description: Excludes categories with no visible products from the WooCommerce category list | |
Version: 0.2 | |
Author: Chris Klosowski | |
Editor: Randall Kam | |
Author URI: https://chrisk.io | |
*/ | |
/** | |
* Exclude categories with no visible products from the category list | |
* | |
* @param array $category_list_args Array of wp_list_categories parameters | |
* @return array Addition of exclude if items are not visible | |
*/ | |
function get_parent_cats ($cat_termid, $found = array()) { | |
array_push ($found, $cat_termid); | |
$term =get_term_by( 'term_id', $cat_termid, 'product_cat'); | |
if($term->parent > 0){ | |
return get_parent_cats($term->parent, $found); | |
} | |
return $found; | |
} | |
function kfg_exclude_categories_from_widget( $category_list_args ) { | |
$current_tax = get_query_var( 'product_cat' ); | |
$term =get_term_by( 'slug', $current_tax, 'product_cat'); | |
$term_id = $term->term_id; | |
$parents = get_parent_cats($term_id); | |
$args = array( | |
'hide_empty' => false, | |
'hierarchical' => true, | |
); | |
$product_categories = get_terms( 'product_cat', $args ); | |
$exclude = array(); | |
foreach ( $product_categories as $category ) { | |
$posts = get_posts( array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $category->slug, 'fields' => 'ids' ) ); | |
$show_category = false; | |
foreach ( $posts as $post ) { | |
$product = new wC_Product( $post ); | |
$visible_product = $product->is_visible(); | |
if ( true === $visible_product ) { | |
$show_category = true; | |
break; | |
} | |
} | |
if ( false === $show_category || ( $category->parent > 0 && !in_array($category->parent,$parents) ) ) { | |
$exclude[] = $category->term_id; | |
} | |
} | |
if ( ! empty( $exclude ) ) { | |
$category_list_args['exclude'] = implode( ',', $exclude ); | |
unset( $category_list_args['include'] ); | |
} | |
return $category_list_args; | |
} | |
add_filter( 'woocommerce_product_categories_widget_args', 'kfg_exclude_categories_from_widget', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment