Custom order by
-
I am looking for a way to order by attributes from low to high and high to low. I tried adding this to my functions.php but it will order by something (always the same order) but no idea what it orders by.
So this is what i am working with at the moment, so far it comes up in the orderby function from woocommerce. And it tries to sort something but not the attribute i am trying to sort.
‘nominaal-koppel-nm’ is the attribute that in this case i am trying to sort by number. A value of an attribute like this would look like 1.5 [Nm] so not sure if it actually counts as a number since there are letters behind it.
//add new options to $sortby var passed into filter function custom_woocommerce_catalog_orderby($sortby) { $sortby['koppel_asc'] = "Sorteer op Koppel H-L"; $sortby['koppel_desc'] = "Sorteer op Koppel L-H"; return $sortby; } //Attach our function to the filter hook: add_filter('woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args'); //Function to handle choices function custom_catalog_ordering_args($args) { global $wp_query; // Changed the $_SESSION to $_GET if ($_GET['orderby'] == "koppel_asc") { $args['meta_key'] = 'nominaal-koppel-nm'; $args['orderby'] = 'meta_value_num'; $args['order'] = "ASC"; } else if ($_GET['orderby'] == "koppel_desc") { $args['meta_key'] = 'nominaal-koppel-nm'; $args['orderby'] = 'meta_value_num'; $args['order'] = "DESC"; } return $args; }
I have seen so many questions really similar like this on the internet but no one can really explain how to get this done.
Thanks for your time ,
Sjoerd
- The topic ‘Custom order by’ is closed to new replies.