4
votes

I need to separate all the products that I have on woocommerce shop page into categories. As default, even though products belong to different categories all comes up in a same list. Would be great to have each category in a separate lists under the category title.

2
I'm also looking for this. Any luck?Daniel Holm

2 Answers

3
votes

The only way to do this would be to copy and edit the template files or to use hooks. My favorite way is to use hooks because it is way less intrusive for when they do a plugin update. First you want to make sure that you have your woocommerce settings set to display categories on your product pages. Then open your themes functions.php file and place this in it:

function order_by_multiple() {
if(function_exists('is_woocommerce')){
if(is_woocommerce()||is_search()||is_product_category())    return ' tm.meta_value, post_title';
}
}
function product_order_join($join){
global $wpdb;
if(function_exists('is_woocommerce')){
if(is_woocommerce()||is_search()||is_product_category()){
$join.= " JOIN " . $wpdb->term_relationships ." tr ON " . $wpdb->posts . ".id = tr.object_id JOIN " . $wpdb->term_taxonomy ." tt ON tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy =  'product_cat' JOIN " . $wpdb->terms ." t ON tt.term_id = t.term_id
 join " . $wpdb->woocommerce_termmeta ." tm on tm.woocommerce_term_id = t.term_id and tm.meta_key = 'order' ";}
 }
 return $join;

}
add_filter("posts_join","product_order_join");
if(!is_admin())add_filter("posts_orderby", "order_by_multiple");

Replace 'product_cat' with whatever the taxonomy/attribute is. For attributes, woocommerce ads a 'pa_' in front for the taxonomy name. So, for if the name of your attribut was genre, then you would replace product_cat with pa_genre.

also, you can probably replace

add_filter("posts_orderby", "order_by_multiple");

with

add_filter('woocommerce_get_catalog_ordering_args',"order_by_multiple");

I just haven't had the chance to test that yet.

Lastly you could by pass almost all of that stuff and use the woocommerce short code of:

[product_category category="category-slug-name-here" per_page="12" columns="4" orderby="date" order="desc"]
0
votes

I just stuck this straight into my page and it worked fine -

<?php echo do_shortcode( '[product_category category="category name"]'); ?>