WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Sort products by product attributes or dimensions (4 posts)

  1. hegero
    Member
    Posted 1 year ago #

    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'] = '';
    			break;
    			case 'price_desc' :
    				$args['orderby'] = 'meta_value_num';
    				$args['order'] = 'desc';
    				$args['meta_key'] = '_price';
    			break;
    			case 'title_desc' :
    				$args['orderby'] = 'title';
    				$args['order'] = 'desc';
    				$args['meta_key'] = '';
    			break;
    		endswitch;
    	}
    	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!

    http://wordpress.org/extend/plugins/woocommerce/

  2. John
    Member
    Posted 1 year ago #

    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.

  3. GalalAly
    Member
    Posted 1 year ago #

    I did a simple work around and wrote a blog post about it here .. http://www.galalaly.me/index.php/2013/05/woocommerce-sort-by-custom-attributes/

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

  4. hegero
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic