Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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.

share|improve this question
    
did you check woocommerce documentation? wcdocs.woothemes.com/snippets/… –  ronald Feb 25 '13 at 23:32
add comment

2 Answers

up vote 1 down vote accepted

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;' ) );
}
share|improve this answer
    
That's the trick. Many thanks –  harryg Feb 26 '13 at 16:54
add comment

Just read your topic,

I've created a plugin around this to create a dropdown box for the visitor to select the number of products he/she wants to see.

http://wordpress.org/plugins/woocommerce-products-per-page/

Hope you can use this in your situation.

share|improve this answer
add comment

protected by Community Jul 21 '13 at 22:49

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.