N
N
Nik Master2020-05-17 16:04:42
WordPress
Nik Master, 2020-05-17 16:04:42

How to hide woocommerce subcategories?

How to hide subcategories in woocommerce? It is necessary that when switching to a category, its subcategories are not displayed. I changed the settings in woocommerce itself, but they do not work. Most likely they do not work due to the fact that there is a code for displaying categories above products (The code itself is below). Please tell me how to resolve the issue.

function tutsplus_product_subcategories( $args = array() ) {

$parentid = get_queried_object_id();

$args = array(
'parent' => $parentid
);

$terms = get_terms( 'product_cat', $args );

if ( $terms ) {

echo '<ul class="product-cats-top">';

foreach ( $terms as $term ) {

echo '<li class="category-top-shop">';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
woocommerce_subcategory_thumbnail( $term );

echo '<h3>'.$term->name.'</h3>';
echo '</a>';
}

echo '</ul>';

}

}
add_action( 'woocommerce_before_shop_loop', 'tutsplus_product_subcategories', 10 );

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nik Master, 2020-05-28
@NikMaster777

Found a solution.
By default, the store page uses the archive-product.php template file. We add our own template for this page, for example, shop.php:

add_filter( 'woocommerce_template_loader_files','add_shop_template_loader_files', 10, 1 );

function add_shop_template_loader_files($default_file){

    if( is_shop()){
        $default_file[] = WC()->template_path() .'shop.php';
    }

    return $default_file;

}

We put the code accordingly in functions.php.
All that's left is to place the shop.php file in your theme's woocommerce folder. If there is no such folder, then you need to create it.

W
WP Panda, 2020-05-17
@wppanda5

remove_action( 'woocommerce_before_shop_loop', 'tutsplus_product_subcategories', 10 );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question