• Resolved Lorangeo

    (@lorangeo)


    Hi,

    Woocommerce categories and products appear on the same row, in the same ul element. I personally find this confusing and was looking for a way to separate them.

    I found this code, which looks like a very clean solution.

    // Remove subcategories from the product loop
    remove_filter( 'woocommerce_product_loop_start', 'woocommerce_maybe_show_product_subcategories' );
    
    // Add subcategories before the product loop with priority 5
    add_action( 'woocommerce_before_shop_loop', 'cg_show_product_subcategories', 5 );
    
    function cg_show_product_subcategories() {
        $subcategories = woocommerce_maybe_show_product_subcategories();
        if ( $subcategories ) {
            echo '<ul class="products subcategories">', $subcategories, '</ul>';
        }
    }

    Source: https://www.commercegurus.com/docs/shoptimizer-theme/products-subcategories-separate/

    The listings pages look much better like this!

    The problem is that on my site (I’m using the Astra free theme), the categories are now showing twice. In their new place (before the shop loop, just above the ordering filter options, which is actually perfect and should be their default place in my opinion), AND in the shop loop alongside with the products (the default place where they already were before).

    Could someone help me find the way to prevent them to show a second time in the shop loop?

    And what’s about make this a default behaviour (or at least an option) in Woocommerce?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support omarfpg a11n

    (@omarfpg)

    Hi @lorangeo,

    I’m glad to learn you’ve figured out part of your display issue with the custom code snippet! Please keep in mind this request is really more oriented towards Astra rather than WooCommerce, as this is related to how the theme displays things. However, since we’re here, allow me to help as a courtesy!

    Please go to your Customizer → WooCommerce → Product Catalog → Shop Display Options → Shop page displays, and select only products. That should remove the categories from the shop loop.

    Feel free to start a new thread if you have any more questions about WooCommerce. If you have any further questions about your theme’s display or options, please open a new thread in the Astra forums.

    Cheers!
    -OP

    Thread Starter Lorangeo

    (@lorangeo)

    Hi Omar,

    Thank you very much for taking the time to answer. I already tried this. But when I select this option (“Show products” (only)), the categories do not show anymore, neither before the shop loop, nor in their original place (inside the shop loop). ??

    Hey, @lorangeo!

    But when I select this option (“Show products” (only)), the categories do not show anymore, neither before the shop loop, nor in their original place (inside the shop loop).

    To confirm, this is happening after you added the code? If you remove the code it shows again?

    As my colleague mentioned, this might be an Astra specific issue, and if it is, they will be better suited to help. But do you mind sharing your website’s URL so we can take a look and make sure?

    Please share the URL of the page you see the categories showing twice.

    Looking forward to your reply.

    Have a wonderful day!

    Thread Starter Lorangeo

    (@lorangeo)

    Yes, with the code that I pasted above the categories show twice (with the woocommerce settings set to “show categories and products”), or do not show anymore (with the woocommerce settings set to “Products (only)”).

    You can see it here: https://hepicerie.com/_hepwordpress/ressources (development site)

    Plugin Support Shameem a11n

    (@shameemreza)

    Hi @lorangeo

    The Astra theme might have its own functions for displaying categories, and when you add this code, it could cause the categories to be called twice – once by the theme and once by your added code. You could try reaching out to Astra support to see if they have any insights or solutions to this issue, as it seems to be explicitly related to the theme.

    For reference, this particular forum is meant for general support with the core functionality of WooCommerce itself. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Thread Starter Lorangeo

    (@lorangeo)

    Hello again!

    As suggested by the Astra team, I tried to temporarily test this with the default WooCommerce theme (Front Store), and it does exactly the same! It shows the categories twice! First,?before the shop loop?(just above the ‘Sort Products’ dropdown), and a second time?alongside the products (their original place).

    So it’s definitely not an Astra issue.

    The code

    remove_filter( 'woocommerce_product_loop_start', 'woocommerce_maybe_show_product_subcategories' );

    doesn’t seem to work as expected.

    As I already said, I think it would be much more logical to show the categories before the shop loop (above the ‘Sort Products’ dropdown), instead of showing them alongside the products. It really makes things much more clear (just try it on your own shop!). So it would be great if someone could help make the above code work as expected. ??

    Plugin Support Shameem a11n

    (@shameemreza)

    Hi @lorangeo

    Thanks for keeping us in the loop with these updates.

    I tried to recreate the issue you’re experiencing using both the Storefront and Twenty Twenty-Four themes. However, everything seemed to work fine on my end. When I used the reference code snippet you shared, the Category appeared at the top, and the products were displayed below it without any duplication.

    When I tested the Astra theme, I noticed it automatically displayed the category name above the product title. To separate the category and product listings, I added the provided code snippet. However, due to the theme’s default behavior, the category name will still appear above the category title. You can easily remove this from the customizer in the Shop Card Structure section.

    Furthermore, as said before, writing or providing custom code is not within the scope of our support policy. If you are still having problems, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.

    Thread Starter Lorangeo

    (@lorangeo)

    Hi!

    Thank you so much for taking the time to look at this. I don’t know why, but with my Astra theme the product categories were appearing twice (before and inside the shop loop), despite deactivating all other plugins and custom code snippets.

    After some troubleshooting, I decided to adjust the display settings in the Customizer. I set the “Shop page display” to “Show products” only and the “Category display” to “Show products” only. With this approach and my original code, the categories were no longer appearing either before or inside the shop loop.

    Next, I made some modifications to the code as follows:

    add_action( 'woocommerce_before_shop_loop', 'cg_show_product_categories', 10 );
    
    function cg_show_product_categories() {
        // Check if we're on a category page
        if ( is_product_category() ) {
            // Output subcategories for the current category
            woocommerce_output_product_categories( array(
                'parent_id' => get_queried_object_id(),
                'before'    => '<ul class="products subcategories">',
                'after'     => '</ul>',
            ) );
        } else {
            // Output root categories for the shop page
            woocommerce_output_product_categories( array(
                'before' => '<ul class="products subcategories">',
                'after'  => '</ul>',
            ) );
        }
    }
    

    And voilà! It works now as expected. With these changes, the categories (or subcategories) are displayed correctly – before the shop loop only!

    I will mark this topic as resolved. Thank you very much for the help!

    Plugin Support omarfpg a11n

    (@omarfpg)

    Hi @lorangeo,

    And voilà! It works now as expected. With these changes, the categories (or subcategories) are displayed correctly – before the shop loop only!

    Thank you for sharing your approach to solving this. This information may come in handy for other merchants facing the same issue!

    I will mark this topic as resolved. Thank you very much for the help!

    I’m glad we were able to help! If you have a few minutes, we’d love if you could leave us a review:?https://www.ads-software.com/support/plugin/woocommerce/reviews/.

    Cheers!
    -OP

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.