• sample image

    hi,
    i have a question about adding Button (or only text link) like on the image in attached link.
    So when clicking on button will display all 15 products in 3 Subcategorys.

    i tried with some code in functions.php but without succes.
    Hope someone can help me with this..

    Thanks!
    Dusan V.
    SLOVENIA

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi there, what code did you try so far in functions.php? Additionally, where are you wanting this button to show up on the site?

    On the homepage or some other page where products are listed?

    Thread Starter dusko1

    (@dusko1)

    Hi again.
    i want to show button on specific main category. Not on shop page or home page.
    i tried the code attached. It show “Show all” but it happens nothing, it doesnt show then all products in specific category…

    i used this code:

    /**
    * 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(
    %s“,
    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;
    }

    The code you posted is missing the <a> element to create the link, try this code instead as I just tested on my local Storefront install and it appears to work for me

    /**
    * 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;
    }
    Thread Starter dusko1

    (@dusko1)

    Doesn work on my site… Button Showw all disappear when clickin, but all products are not shown…

    see here:
    https://delta-kom.net/delta-shop/kategorija-izdelka/gsm-oprema/gsm-oprema-po-modelu-telefona/apple-gsm-oprema/apple-iphone-11/

    it should show all products from 3 subcategories…

    Thread Starter dusko1

    (@dusko1)

    when in chrome open button in new tab it shows all products…

    If you go into Appearance->Customize->WooCommerce->Product Catalog, what do you have set for the Category Display dropdown? If you try setting it to Show products does that get it working to how you’d like?

    Thread Starter dusko1

    (@dusko1)

    no, it doesnt work… ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Button ¨Show all¨ on product category page’ is closed to new replies.