Reputation: 1
I am using Woocommerce 3.3.3 and trying to change the number of columns for a specific product category and not the other product categories. I have tried several code options, but they all apply the change in columns to all categories. Here is one of thing I've tried.
/* change columns to 1 for newsletter issues prod cat*/
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
function loop_columns() {
if ( is_product_category('newsletter-issues')) {
return 1;
} else { // for other archive pages and shop page
return 4;
}
}
}
Upvotes: 0
Views: 2974
Reputation: 1
This works to override the number of products for a specific category, just replace the CATEGORY and NUMBER in the function.
/* Set products per page for specific category */
add_filter( 'loop_shop_per_page', 'products_per_page_category');
if (!function_exists('products_per_page_category')) {
function products_per_page_category( $count ) {
if (is_product_category('CATEGORY')) {
return NUMBER;
} else {
return $count;
}
}
}
Upvotes: 0
Reputation: 1
As per the @Codextent code, In product columns for specific category It doesn't work, I think it is caused by the woocommerce new verison. currently I am using 5.0 version of woocoomerce.
the other showing product per page is working
here it seems,
if ( is_product_category('a')) {
return 3;
} else if ( is_product_category('c')) {
return 2;
}else{
return 4;
}
To work, changed above code to this
if ( has_term( 'a', 'product_cat' ) ) {
return 4; // 4 products per row
}else{
return 2;
}
Happy Coding :)
Upvotes: 0
Reputation: 444
Using the bellow function you can set product list column and number of product per category page.
/* Set product columns for specific category */
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
function loop_columns() {
if ( is_product_category('a')) {
return 3;
} else if ( is_product_category('c')) {
return 2;
}else{
return 4;
}
}
}
/* Set product per page for specific category */
add_action( 'woocommerce_product_query', 'cdx_woocommerce_products_per_page', 1, 50 );
function cdx_woocommerce_products_per_page( $query ) {
if( $query->is_main_query() && isset($query->query['product_cat'])){
switch($query->query['product_cat']){
case 'a':
$query->set( 'posts_per_page', '3' );
break;
case 'c':
$query->set( 'posts_per_page', '2' );
break;
default:
$query->set( 'posts_per_page', '4' );
}
}
}
Upvotes: 2