Show subcategories broken when WP/WC updates

I’ve been given to mantain a site who wasn’t updated for a while: it had WP 4.9.15 and WooCommerce 2.6.14

Now I’ve made a site test where i’ve updated all these things and now i have WP 5.4.2 and WC 4.3.1

One thing that is broken and i’m not able to figure why is the page showing the products by category: they have a 3 level category tree. It worked this way: if the category asked was a leaf, the page showed the product list. If it wasn’t a leaf, it showed all the subcategories.

The template page doing this is archive-product.php which has this simple loop to show everything:

            <h1 class="page-title"><?php woocommerce_page_title(); ?></h1>

            <?php if ( have_posts() ) : ?>

                <?php
                    /**
                     * woocommerce_before_shop_loop hook
                     *
                     * @hooked woocommerce_result_count - 20
                     * @hooked woocommerce_catalog_ordering - 30
                     */
                    do_action( 'woocommerce_before_shop_loop' );
                ?>

                <?php woocommerce_product_loop_start(); ?>

                    <?php woocommerce_product_subcategories(); ?>

                    <?php while ( have_posts() ) : the_post(); ?>

                        <?php wc_get_template_part( 'content', 'product' ); ?>

                    <?php endwhile; // end of the loop. ?>

                <?php woocommerce_product_loop_end(); ?>

                <?php
                    /**
                     * woocommerce_after_shop_loop hook
                     *
                     * @hooked woocommerce_pagination - 10
                     */
                    do_action( 'woocommerce_after_shop_loop' );
                ?>

            <?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>

                <?php wc_get_template( 'loop/no-products-found.php' ); ?>

            <?php endif; ?>

Now this page works only if the category requested is a leaf thus it has to display a list of products. If the category requested is not a leaf, no subcategory is shown and the page is blank with only the category name (page_title) showing. So it means that the query is somewhat did good, but why no category is shown? Thank you if you can help me solving this trouble

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Found the answer myself, since WC 3.3 woocommerce_product_subcategories is deprecated and returns an empty value:

Link to woocommerce docs

So you have to implement your own subcategories list view. I used as hint the code found here and rebuild the categories page as it was before


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x