• Resolved Suhan Alam Rana

    (@ranakpik)


    I am using woocommerce plugin in my custom theme. By default woocommerce show 4 product per row, but I want to show it according to total product in a category.
    For Example
    If have 3 product in a category than display 3 product in one row.
    If have 6 or 9 product display 3 product per row. or
    If have 8 total product than display 4 product per row.
    if have 12 product or more than display 6 product per row.

    Any idea how to change the number of product per row in woocommerce!!!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The code to change the number of products per row is here:
    https://docs.woocommerce.com/document/change-number-of-products-per-row/

    In your case, inside the function you will want to bring in the query as a global, then use:
    $nr_products = $wp_query->found_posts
    and use a switch/case structure to return the appropriate number.

    Code goes in functions.php.

    You may need to add a custom nr-of-columns classname to the body tag:
    https://developer.www.ads-software.com/reference/functions/body_class/
    This should allow your css to specify different block widths depending on the number of columns, eg:
    .custom-3-cols .products .product {width:33%}
    .custom-4-cols .products .product {width:25%}

    Thread Starter Suhan Alam Rana

    (@ranakpik)

    Hi Iorro.
    Thanks lot for you help. I already put the query function. here is the code
    function my_loop_shop_columns() {
    global $wp_query;
    // calculate the amount of products visible on the current page
    $posts_on_page = min($wp_query->found_posts, $wp_query->get(‘posts_per_page’));
    if($posts_on_page >= 12) return 6;
    if($posts_on_page % 4 == 0) return 4;
    if($posts_on_page % 3 == 0) return 3;
    return 4;
    }
    add_filter(‘loop_shop_columns’, ‘my_loop_shop_columns’);
    this code is working good but how can I add css class according our conditions?
    Sorry i’m new ??

    
    if ($post_on_page >= 12 ) {
      add_filter( 'body_class', function( $classes ) {
        return array_merge( $classes, array( 'custom-6-cols' ) );
      } );
      return 6;
    }
    // and so on
    

    See “add new classes using a filter” here:
    https://developer.www.ads-software.com/reference/functions/body_class/

    Sorry not tested – may need a debug!

    Thread Starter Suhan Alam Rana

    (@ranakpik)

    Hi Iorro
    I’m really glad for your help. I’m successfully dynamically add class for specific category on body tag from your example link. Thank you so much. its my first work on wordpress woocommerce. The code is:

    function wp_body_classes( $classes ) {
    global $wp_query;

    // calculate the amount of products visible on the current page
    $posts_on_page = min($wp_query->found_posts, $wp_query->get(‘posts_per_page’));
    if($posts_on_page >= 12){
    $classes[] = ‘custom-6-cols’;
    return $classes;
    }
    }
    add_filter( ‘body_class’,’wp_body_classes’ );

    Thanks a lots ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to dynamically Change number of products per row – WooCommerce’ is closed to new replies.