• Resolved megosoft

    (@megosoft)


    Hello, I am using your integrated Filter products by attribute, in list mode. This one: https://prnt.sc/uapib5
    I need to order list items by count of items inside. So I am using it to filter clothes sizes:
    S (1)
    M (9)
    L (11)
    XL (8)
    When I save the widget, I get something like this: https://prnt.sc/uapj5n
    I need to order items, that the biggest count will be at the top.

    I tried to look at the woocommerce/includes/widgets/class-wc-layered-nav.php and found something like
    $terms = get_terms($taxonomy, array('hide_empty'=>1));
    I have edited this to
    $terms = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'hide_empty' => '1' ) );

    When I dump the $terms everywhere in code, I am getting the correct order. However, when the content is rendered on site, it is chaotic.
    I think, there is any filter, or something like that, which reorder the items before rendering on site. How can I do it, that the biggest count item will be the first always?

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support RK a11n

    (@riaanknoetze)

    Hi there,

    Can you share a screenshot of the order of the attributes as found under Product > Attributes > Velkost? I’m asking as the order on that page can be dragged-and-dropped, which should affect things on the front-end as well.

    Thanks!

    Thread Starter megosoft

    (@megosoft)

    Hello, thank you for quick reply. Here’s the screenshot: https://prnt.sc/uaqf62

    However, this is a problem, I need to rewrite the order of that attributes.

    Example:
    I have category “Children trousers”, where M size is the biggest (1000 pcs)
    However on category “Children coats” the S size could be bigger than M size. So I need to besafe, that categories will order by count, not by their position in wp-admin. Is that possible?

    Plugin Support RK a11n

    (@riaanknoetze)

    Thanks for that screenshot – looks like quite a complicated setup there ??

    I’m speaking under correction here but I’m not 100% sure that a taxonomy query supports count ordering. As far as I’m aware, that’s only possible on the custom post type query. For more information, have a look at the following: https://wordpress.stackexchange.com/questions/293895/order-posts-by-taxonomy-count

    Thread Starter megosoft

    (@megosoft)

    So when I replace $terms = get_terms(…);
    with that heavymetal query, it should work as well?

    Thread Starter megosoft

    (@megosoft)

    I have just invented ?? that this order is working fine, but problem is, that it concerns to global Woocommerce, not only to current category

    Thread Starter megosoft

    (@megosoft)

    The problem is resolved, after 8 hours of tears, pain and suffering ??
    After line 526 add to new line this:

    foreach($terms as $key => $item) {
    			$id = $item->term_id;
    			if(isset($term_counts[$id])&& !empty($term_counts[$id])) {
    				$terms[$key]->current_count = $term_counts[$id];
    			} else {
    				$terms[$key]->current_count = 0;
    			}
    		}
    		
    		$terms = wp_list_sort($terms, 'current_count', 'DESC');

    The problem is, items concerns to whole woocommerce, not only to current category. So you get a list of whole properties, then, in foreach, unrelevant items are skipped. You need to attach count to $terms before foreach.
    So in my foreach, you are combining two arrays and adding new object’s property (current_count) to each item in $terms. If there is no product on actual category, you will add current_count = 0.
    Then use wp_list_sort function to sort array items by “current_count” after that, you will get your filter items order by count descending ??

    https://prnt.sc/ubczju

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Filter product by attribute, can’t order items’ is closed to new replies.