Thank you very much.
Wouldn’t you know how to hide attributes with no visible products? I am using setting to hide out of stock products in Woocommerce, so actually attributes with no in stock products.
For example, for hiding categories in widget I use this snippet:
function kfg_exclude_categories_from_widget( $category_list_args ) {
$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_in_stock();
if ( true === $visible_product ) {
$show_category = true;
break;
}
}
if ( false === $show_category ) {
$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 );
It hides product categories with no visibile products in widget.
-
This reply was modified 5 years, 7 months ago by fkoomek.