Forked from cklosowski/woocommerce-hide-empty-categores.php
Last active
September 12, 2017 07:46
-
-
Save DarkAllMan/6f4398d5968594aa706a6bfa8558a069 to your computer and use it in GitHub Desktop.
In addition to Chris, I added functionality that child categories will only expand if they are in the selected category.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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