skip to Main Content

I am building WooCommerce store with storefront child theme. In my shop page (archive-product.php) I’m using following code to display products:

<?php
        $filterArray = explode("/", "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI]);


        if(count($filterArray)>5) {
            echo do_shortcode('[products category="' . $filterArray[count($filterArray)-2] . '" per_page="30" limit="30" columns="3" paginate="true"]');
        }
        else {
            echo do_shortcode('[products per_page="30" limit="30" columns="3" paginate=true]');
        }
?>

When I don’t pass category attribute to the shortcode, everything works as expected. But when I add category, my page display not 30, but only 2 products per page. Changing products to product category does not work at all. What could be the reason of such strange behaviour?

EDIT: I have some mess in my code, found following function in functions.php:

function modify_product_cat_query( $query ) {
    if (!is_admin() && $query->is_tax("product_cat")){
        $query->set('posts_per_page', 2);
    }
}
add_action( 'pre_get_posts', 'modify_product_cat_query' );

I thought that I finally found the issue, but even when I delete that sinppet or change value from 2 to 30, category page still displays only two products…

2

Answers


  1. You can use woocommerce_product_query to change posts_per_page. check the below code.

    add_action( 'woocommerce_product_query', 'change_posts_per_page', 999 );
    function change_posts_per_page( $query ) {
    
        if( is_admin() )
            return;
    
        $query->set( 'posts_per_page', 30);
        
    }
    
    Login or Signup to reply.
  2. Try using this. You are on archive page so it is more easy to get current term. You don’t have to fetch it from url.

        $term = get_queried_object();
    
        if( !empty( $term ) && !is_wp_error( $term ) ) {
            echo do_shortcode('[products category="' . $term->slug . '" per_page="6" limit="30" columns="3" paginate="true"]');
        } else {
            echo do_shortcode('[products per_page="30" limit="30" columns="3" paginate=true]');
        }
    

    The issue you are facing product are not displaying more than 2 it’s probably because there is something overriding posts_per_page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search