WordPress.org

Support

Support » Plugins and Hacks » WooCommerce » [Plugin: WooCommerce] Trying to "order" categories

[Plugin: WooCommerce] Trying to "order" categories

Viewing 15 replies - 1 through 15 (of 21 total)
  • Plugin Author Mike Jolley

    @mikejolley

    Go to the product category page in admin and simply drag and drop 🙂

    Would there be any way I would be able to sort the default Categories and Products with PHP with out dragging and dropping?

    You may need to add a posts_orderby filter if you want to some kind of custom sorting. I added a couple functions to do this so I could display all products and still sort them by category.

    Thanks for the quick reply! Do you know where I can view a reference of this?

    I found this and did this the job!

    add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
    function am_woocommerce_catalog_orderby( $args ) {
    	$args['orderby'] = 'date';
    	$args['order'] = 'desc';
    	$args['meta_key'] = '';
        return $args;
    }

    wow, I didn’t even know about that filter.

    Can I make this work with attributes?

    Thanks

    oh, wait… that snippet up there only orders by date – if you actually want to order products by category (or any other taxonomy, for that matter) you need to do a little sql like so:

    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 it yet.

    Is that what you’re looking for Marce? or are you want to override the order that attributes are displayed on the single product page?

    @bheadrick, thanks for trying, but it’s now working for me. I replaced ‘product_cat’ with my custom taxonomy (pa_provincia) but something when wrong. Products do not show on home/shop page, I get the “Nothing found” message.

    Still, the taxonomies drop downs didn’t affect the order of the attributes. Silly question, that code goes into functions.php, right?

    Thanks!

    it does goes in the functions.php page, as for your other questions I am out of the loop.

    Ok, thanks

    Hello guys, how can I order the products by categories on a shop page? On my shop page (using Wootique free theme) the products appears just in list. I would like to order them in Categories.

    Thanks for the answer in advance,

    Zoltan

    texins5

    @texins5

    This works to sort products by category on the main shop page, but it also changes the order of my blog posts AND takes away the ability for any other sort options, which is a bit of a bummer. Can anyone take a look at this and tell me if there’s a way to tweak it so it works better? Unfortunately I can’t write this stuff from scratch. I have to find something and make it work.

    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");
    texins5

    @texins5

    I also found Woo’s snippet for adding some other asc/desc sort options. Maybe there’s some way to add this same thing for sorting by category? I messed around with it, but I only know enough to be dangerous. Does this give anyone some good ideas?

    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;
    }
    danny80

    @danny80

    Hi

    I am new, can some one please let me know to which file should I add above code in order to sort my products by category?

    Thanks

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘[Plugin: WooCommerce] Trying to "order" categories’ is closed to new replies.
Skip to toolbar