Support » Plugin: WooCommerce » Sort products by product attributes or dimensions

  • Is it possible to sort products by attribute or product dimension? I have products with an attribute called “Undergr.” (the values are numbers, different value for each product) and I’m trying to have an option to sort by this value ascending and/or descending.

    I found the code snippet on WooCommerce Document that shows how to apply custom sorting options:

     * This code should be added to functions.php of your theme
    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'] = '';
    			case 'price_desc' :
    				$args['orderby'] = 'meta_value_num';
    				$args['order'] = 'desc';
    				$args['meta_key'] = '_price';
    			case 'title_desc' :
    				$args['orderby'] = 'title';
    				$args['order'] = 'desc';
    				$args['meta_key'] = '';
    	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;

    I’ve tried using the meta_value_num example, and change the meta_key, but nothing I’ve tried so far is working and I don’t understand why.

    A possible alternative is to sort by dimensions, either length, height or width, or all three of them. Is that possible?

    This is really driving me crazy, any help is greatly appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • I wanted to do the same thing, but it looks like that example is for sorting by meta_value.

    WIth product attributes the issue appears to be more complicated as they are stored in a serialized format in the database as the ‘_product_attributes’ meta_key value if that makes sense.

    From what I can tell, it will be a significantly more involved effort to get WC sorting according to the _product_attributes values themselves.

    I did a simple work around and wrote a blog post about it here ..

    What i did is saving the submitted custom attributes to the database as post meta data so that I can query them.

    Thread Starter hegero


    @john That makes sense!
    @galalaly Thanks a lot! I will definitely try your work around.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sort products by product attributes or dimensions’ is closed to new replies.