0

I want to add view all button on my woo-commerce product page after pagination.after clicking the view all button all product will be open.

  • Can you please let me know website URL or with screenshot where exactly you want to display this button so I can clearly understand and will help you. – Tanmay Patel Apr 2 at 6:28
  • after pagination i want to add view all please help me out – saurabh srivastava Apr 2 at 6:50
  • My code is working for you or not??? Please let me know. – Tanmay Patel Apr 2 at 17:30
  • no sir your code is not working but i try another way to do this add some function on function.php now it's working – saurabh srivastava Apr 3 at 5:07
0

Put below code after this action <?php do_action('woocommerce_after_shop_loop' ); ?> in archive-product.php file.

For Category Products Page

<?php
$cat_data = get_queried_object();
$cat_id = $cat_data->term_id;
if(is_product_category()  && !isset($_GET['showall'])){ ?>
    <a href="<?php echo get_term_link( $cat_id, 'product_cat' ) . "?showall=1"; ?>">Show all</a>
<?php } ?>

For Shop Page

<?php if(is_shop() && !isset($_GET['showall'])){ ?>
    <a href="<?php echo get_permalink( woocommerce_get_page_id( 'shop' ) ) . "?showall=1"; ?>">Show all</a>
<?php } ?>

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 {
    $default_posts_per_page = get_option( 'posts_per_page' );
    add_filter( 'loop_shop_per_page', create_function( '$cols', 'return '.$default_posts_per_page.';' ) );
}
0

I will offer you a different approach, without the need to actually edit the template files. All you need to do is add those two action hooks to your function.php file (make sure you are using a child theme):

/**
 * This will add a 'Show All' link after the pagination on the shop pages.
 * It will be hidden once it is activated.
 */
add_action( 'woocommerce_after_shop_loop', 'wpse333192_add_showall', 40 );

function wpse333192_add_showall() {

    if ( ! isset( $_GET['showall'] ) ) {
        global $wp;

        echo sprintf(
            "<a href='%s'>%s</a>",
            home_url( add_query_arg( array_merge( $_GET, [ 'showall' => 1 ] ), $wp->request ) ),
            __( 'Show All', 'text-domain' )
        );
    }

}

/**
 * This will alter the main product query if 'showall' is activated
 */
add_action( 'pre_get_posts', 'wpse333192_alter_query_showall' );

function wpse333192_alter_query_showall( $query ) {

    /**
     * Alter the query only if it is:
     * 1. The main query
     * 2. Post type is product
     * 3. $_GET['showall'] is set
     * 4. $_GET['showall'] equals 1
     */
    if ( $query->is_main_query()
         && $query->get( 'post_type' ) == 'product'
         && isset( $_GET['showall'] )
         && $_GET['showall'] == 1
    ) {
        // Load the 'first' page
        $query->set( 'paged', 1 );

        // Set post per page to unlimited
        $query->set( 'posts_per_page', - 1 );
    }

    return $query;
}
  • thank you so much it's working perfectly on my website – saurabh srivastava Apr 3 at 5:04
  • I am really glad to hear that. If you like an answer, you should give it the upvote. And don't forget to mark the best answer that solves your problem as well. :) – dboris Apr 3 at 5:19
  • i have another question can you help me out – saurabh srivastava Apr 3 at 5:43
  • Yes, but if it is not completely related to this one, you should start another thread by crrating a new question. – dboris Apr 3 at 5:45
  • i already create new question please check . – saurabh srivastava Apr 3 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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