• 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

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

    (@riaanknoetze)

    Why do that programmatically when you can do it in WP Admin under **Products > Attributes > YOUR ATTRIBUTE > Configure Terms** by dragging and dropping them in your desired order?

    Thread Starter sjoerd89

    (@sjoerd89)

    You kind of missing the point here, i am making the standard order by into ordering by attributes. Having the attributes in order in the backend won’t make them order by that option.

    Thanks for the reply tho,
    still need the answer ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom order by’ is closed to new replies.