WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
[Plugin: WooCommerce] Trying to "order" categories (22 posts)

  1. ryan@adeptinkandpixel.com
    Member
    Posted 2 years ago #

    Right now I display all the main product categories on my shop page. However it seams as though they are in DESC order, I would love for them to be in ASC order.

    Any word on how I can do this?

    Thanks!

    http://wordpress.org/extend/plugins/woocommerce/

  2. mikejolley
    Member
    Plugin Author

    Posted 2 years ago #

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

  3. silentfxx
    Member
    Posted 1 year ago #

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

  4. bheadrick
    Member
    Posted 1 year ago #

    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.

  5. silentfxx
    Member
    Posted 1 year ago #

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

  6. silentfxx
    Member
    Posted 1 year ago #

    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;
    }
  7. bheadrick
    Member
    Posted 1 year ago #

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

  8. MarceFX
    Member
    Posted 1 year ago #

    Can I make this work with attributes?

    Thanks

  9. bheadrick
    Member
    Posted 1 year ago #

    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?

  10. MarceFX
    Member
    Posted 1 year ago #

    @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!

  11. silentfxx
    Member
    Posted 1 year ago #

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

  12. MarceFX
    Member
    Posted 1 year ago #

    Ok, thanks

  13. b4press
    Member
    Posted 1 year ago #

    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

  14. texins5
    Member
    Posted 1 year ago #

    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");
  15. texins5
    Member
    Posted 1 year ago #

    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;
    }
  16. danny80
    Member
    Posted 1 year ago #

    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

  17. bheadrick
    Member
    Posted 1 year ago #

    it goes into your theme's functions.php (at the bottom, before the ?>)

  18. jmek
    Member
    Posted 1 year ago #

    If you're creating a shop page using shortcodes, you can designate the ordering within the shortcode:

    [product_categories number="" orderby="title" order="asc"]

  19. texins5
    Member
    Posted 1 year ago #

    I tried to use the shortcode for "products by category slug"

    [product_category category="" per_page="12" columns="4" orderby="date" order="desc"]

    but nothing shows up on my test page. I definitely do have categories with slugs set up, so I'm not sure what the problem is.

    http://barricadeudt.com/store-test/

  20. rhonz10
    Member
    Posted 1 year ago #

    missing category slug

    Ex:
    [product_category category="zombie-contingency" per_page="12" columns="4" orderby="date" order="desc"]

  21. danny80
    Member
    Posted 1 year ago #

    Thanks A lot every body

  22. cochin
    Member
    Posted 1 year ago #

    Any chance to order by sku?
    Can't find a solution for this problem.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic