• I would really like my shop page to be sorted by product category, rather than alphabetically. It’s fine for alphabet sorting to be an option, but I would prefer the default to be sorting by product category.

    Has anyone had any success doing this? I found this little snippet for adding sort options, but I don’t know if there’s a way to tweak this, or if there’s something totally different I should try.

    add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');
    
    function custom_woocommerce_get_catalog_ordering_args( $args ) {
    	if (isset($_SESSION['orderby'])) {
    		switch ($_SESSION['orderby']) :
    			case 'date_asc' :
    				$args['orderby'] = 'date';
    				$args['order'] = 'asc';
    				$args['meta_key'] = '';
    			break;
    			case 'price_desc' :
    				$args['orderby'] = 'meta_value_num';
    				$args['order'] = 'desc';
    				$args['meta_key'] = '_price';
    			break;
    			case 'title_desc' :
    				$args['orderby'] = 'title';
    				$args['order'] = 'desc';
    				$args['meta_key'] = '';
    			break;
    		endswitch;
    	}
    	return $args;
    }
    
    add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
    
    function custom_woocommerce_catalog_orderby( $sortby ) {
    	$sortby['title_desc'] = 'Reverse-Alphabetically';
    	$sortby['price_desc'] = 'Price (highest to lowest)';
    	$sortby['date_asc'] = 'Oldest to newest';
    	return $sortby;
    }
Viewing 1 replies (of 1 total)
  • Thread Starter texins5

    (@texins5)

    Also, I did find this for sorting by category, but it seems to take away all the other sort options, and it also messed up the order of my blog posts. Any help would be greatly appreciated.

    Here’s the website I’m working on:
    https://barricadeudt.com

    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");
Viewing 1 replies (of 1 total)
  • The topic ‘WooCommerce: How Do I Sort by Product Category?’ is closed to new replies.