Separate all products to their categories on Woocommerce shop page

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.

Related posts

Leave a Reply

2 comments

  1. 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"]