[Support request] Controlling Woocommerce products per-row on certain category pages

Home Forums Support [Support request] Controlling Woocommerce products per-row on certain category pages

Home Forums Support Controlling Woocommerce products per-row on certain category pages

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #508015
    Mike

    Hello there!

    I tried the following code placed in functions.php but it does not seem to have an effect. I wonder if the code is wrong, or if GP Woocommerce per-row settings overwrite it, in which case I altered the priorities but no cigar.

    Here’s the code:

    if ( ! function_exists( 'loop_columns' ) ) {
        function loop_columns( $columns ) {
            if ( ! is_product_category( 'target-product-category-here' ) ) return $columns;
                return 5; // Or any other number
        }
    }
    add_filter( 'loop_shop_columns', 'loop_columns' );
    #508401
    Leo
    Staff
    Customer Support

    Hi there,

    Can you try Tom’s code here with your conditional statement?
    https://generatepress.com/forums/topic/customize-woocomerce-layout/#post-225844

    Let us know.

    #513502
    Mike

    Thanks for the reference. I tried the following:

    add_filter('loop_shop_columns', 'loop_columns');
    if (!function_exists('loop_columns')) {
      if (!is_product_category('target-category-here')) {
    	function loop_columns() {
    		return 2; 
      }
    	}
    }

    and

    add_filter('loop_shop_columns', 'loop_columns');
    if (!is_product_category('audio')) {
    	function loop_columns() {
    		return 2; 
    	}
    }

    Neither had any effect. The category pages don’t change the number of columns at all. This problem is a bit of a head-scratcher. Some categories have only two subcategories, so I didn’t want them to be arranged in four columns per row as that would leave two empty spaces. Blowing up those two subcategories to take up the entire area of two columns would be better than the current set-up.

    Some categories need to display 3 columns, others 4 and 2 etc. So I would be targetting specific categories with each block of conditionals. I couldn’t create an array as coding isn’t my strength (I’ve seen them used in different ways before).

    I appreciate any guidance to get this solved!

    #514081
    Tom
    Lead Developer
    Lead Developer

    Give this a shot:

    add_filter( 'option_generate_woocommerce_settings', 'tu_custom_wc_columns' );
    function tu_custom_wc_columns( $options ) {
        if ( ! is_product_category( 'audio' ) ) {
            $options['columns'] = 2;
        }
    
        return $options;
    }
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.