Fixing WooCommerce Pagination Issues in Deep Subcategories

Super excited to share this fix with you!

If you’ve encountered 404 errors when navigating deep subcategories in WooCommerce product archives, you’re not alone. This issue occurs when WooCommerce misinterprets query parameters or struggles with category hierarchies.

Problem Breakdown

If this sounds familiar, here’s a simple function and JavaScript snippet to fix it.

Step-by-Step Fix

1. Modify WooCommerce Query to Handle Deep Subcategories

I use WPCode Lite to add functions.

Add the following PHP function to ensure WooCommerce correctly processes pagination within sub-subcategories:


function fix_subcategory_pagination($query) {
    if (!is_admin() && $query->is_main_query() && is_product_category()) {
        $paged = get_query_var('paged') ? absint(get_query_var('paged')) : 1;
        $query->set('paged', $paged);
        $query->set('posts_per_page', 12); // Adjust as needed

        if ($query->is_archive) {
            $query->set('tax_query', array(
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'slug',
                    'terms'    => get_query_var('product_cat'),
                    'include_children' => true,
                ),
            ));
        }
    }
}
add_action('pre_get_posts', 'fix_subcategory_pagination');
    

What This Does:

2. Add JavaScript to Improve Loading Behavior

Insert the following JavaScript code in Code Snippets:


jQuery(document).ready(function($) {
    var page = 2;
    var loading = false;

    $(window).scroll(function() {
        if ($(window).scrollTop() + $(window).height() >= $(document).height() - 200 && !loading) {
            loading = true;
            $.ajax({
                url: '/wp-admin/admin-ajax.php',
                type: 'POST',
                data: {
                    action: 'load_more_products',
                    page: page
                },
                success: function(response) {
                    $('.products').append(response);
                    page++;
                    loading = false;
                }
            });
        }
    });
});
    

What This Does:

3. Flush Permalinks

After adding the PHP function:

  1. Go to WordPress DashboardSettingsPermalinks
  2. Click Save Changes (without modifying anything)
  3. Test pagination for deeper subcategories

4. Test Pagination Behavior

Try visiting: https://yourdomain.com/product-category/your-sub-category/page/2/

If the page loads properly without a 404 error, the fix worked!

Final Thoughts

This method ensures WooCommerce correctly handles pagination in sub-subcategories, eliminating frustrating errors when navigating deep product listings.

Leave a Reply

Your email address will not be published. Required fields are marked *

Partner with us to elevate your brand and captivate your audience.

Contact Info

© 2025 Destinos Online