• Resolved deandid

    (@lcm404)


    I would like to order the links found in the Categories Widget (commonly placed in the sidebar) by the number of products in each category.

    Is there a hook or plugin that can help accomplish this?

    Thanks in advance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Claudio Sanches

    (@claudiosanches)

    Hello @lcm404,

    It’s possible to sort those items by the number of products in each category using code, but first I need to warning you that WooCommerce uses WordPress functions to generate those widgets, so while you can order by the number of products note that children categories aren’t considered in case your widget is showing hierarchy, only the top level (parent) categories will be taking into account, not the sum of all children categories.

    Here the code that you need to order by the number of products:

    add_filter( 'woocommerce_product_categories_widget_args', function( $args ) {
    	$args['orderby'] = 'count';
    	$args['order']   = 'desc';
    	return $args;
    });
    

    I hope it helps you.

    Thread Starter deandid

    (@lcm404)

    Any reason why this wouldn’t work?

    Robert Ghetau

    (@robertghetau)

    Hi there,

    I’ve tested the code above using the Code Snippets plugin and it re-ordered the categories accordingly in the Product categories widget.

    If you’ve added the code in another way, please give this a shot and let us know how it went.

    Best wishes,

    Plugin Contributor Claudio Sanches

    (@claudiosanches)

    Hello @lcm404,

    Any reason why this wouldn’t work?

    Are you asking why this only work without hierarchy in the widget?
    It’s something default from WordPress, there’s some core tickets about it trying to fix it, but WordPress always worked like that.

    Thread Starter deandid

    (@lcm404)

    Ok, I got it. Was confused but now I understand.

    Robert Ghetau

    (@robertghetau)

    Hey

    Glad we were able to clarify this!

    If you have any requests please start a new thread and we’ll be happy to help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Categories Widget Link Order’ is closed to new replies.