Show all products on one page with WooCommerce

I have WooCommerce running on this store.

Although the default is to show 12 products on a page the client has asked for a “show all” button that will prevent the user having to use pagination to get to the other products.

Read More

Is there a function I can run on the current page that will rerun the loop but change the query to give more ‘posts per page’? I’ve had a poke around the WooCommerce template files but it’s not obvious how this is done.

I was thinking on passing a php GET variable to the current page and just testing for it to determine whether to run the modified query, like so (outside the loop):

<a href="<?php 
echo get_permalink( $post->ID ) . "?showall=1"; 
?>">Show all</a>

And then have something like this before template files loop

<?php if($_GET['showall'] = 1){ //something here to modify the query (wp_query perhaps?? or a woocommerce filter) } ?>
//the loop as normal

Am I on the right track? Is this something that is easy to achieve in this way. Any guidance on how to implement would be appreciated.

Edit:
OK I’ve had a bit more of a dig around the WooCommerce templates. It looks like archive-product.php is the file I’d like to override.

Now, I already specify the products per page with a filter in my theme’s functions.php as below:

add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 12;' ), 20 );

And Ive added this line in the archive-product.php file which I’ve copied to my theme folder/woocommerce to override the default:

        <?php if($_GET['showall']==1){ 
            add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 8;' ), 30 ); 
        } ?>

When I then visit shop_page/?showall=1 the filter fails to execute. The filter in functions.php seems to dominate despite having a lower priority.

Related posts

Leave a Reply

3 comments

  1. Just add the conditional check to your functions.php file:

    if( isset( $_GET['showall'] ) ){ 
        add_filter( 'loop_shop_per_page', create_function( '$cols', 'return -1;' ) ); 
    } else {
        add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 12;' ) );
    }
    
  2. Late to the party, but if you don’t have to account for php 5.2 it’s safer and more efficient to use a closure:

    add_filter( 'loop_shop_per_page', function ( $cols ) {
        return - 1;
    } );
    

    (See php manual on create_function.)