WordPress.org

Forums

WooCommerce - excelling eCommerce
[resolved] WooCommerce Product Sorting (2 posts)

  1. user1752759B
    Member
    Posted 2 years ago #

    I would like to create more sorting options on my WooCommerce Shop page that allows the customer to view products by:

    http://i.stack.imgur.com/SM4KH.jpg

    As opposed to the default sorting:

    http://i.stack.imgur.com/3J6Iz.jpg

    In my functions.php file:

    // Modify the product sorting.
        function custom_woocommerce_get_catalog_ordering_args( $args ) {
        	if (isset($_SESSION['orderby'])) {
        		switch ($_SESSION['orderby']) :
        		case 'price_asc' :
        		$args['orderby'] = 'meta_value_num';
        		$args['order'] = 'asc';
        		$args['meta_key'] = '';
        		break;
    
        		case 'price_desc' :
        		$args['orderby'] = 'meta_value_num';
        		$args['order'] = 'desc';
        		$args['meta_key'] = '_price';
        		break;
    
        		case 'popularity_asc' :
        		$args['orderby'] = 'rating';
        		$args['order'] = 'asc';
        		$args['meta_key'] = '';
        		break;
    
        		case 'popularity_desc' :
        		$args['orderby'] = 'rating';
        		$args['order'] = 'desc';
        		$args['meta_key'] = '';
        		break;
    
        		case 'sales_asc' :
        		$args['orderby'] = 'sales';
        		$args['order'] = 'asc';
        		$args['meta_key'] = '';
        		break;
    
        		case 'sales_desc' :
        		$args['orderby'] = 'sales';
        		$args['order'] = 'desc';
        		$args['meta_key'] = '';
        		break;
    
        		case 'date_asc' :
        		$args['orderby'] = 'date';
        		$args['order'] = 'asc';
        		$args['meta_key'] = '';
        		break;
    
        		case 'date_desc' :
        		$args['orderby'] = 'date';
        		$args['order'] = 'desc';
        		$args['meta_key'] = '';
        		break;
    
        		case 'title_asc' :
        		$args['orderby'] = 'title';
        		$args['order'] = 'asc';
        		$args['meta_key'] = '';
        		break;
    
        		case 'title_desc' :
        		$args['orderby'] = 'title';
        		$args['order'] = 'desc';
        		$args['meta_key'] = '';
        		break;			
    
        		endswitch;
        	}
    
        	return $args;
        }
    
        add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');
    
        function custom_woocommerce_catalog_orderby( $sortby ) {
        	$sortby['price_asc'] = 'Price (Lowest to Highest)';
        	$sortby['price_desc'] = 'Price (Highest to Lowest)';
        	$sortby['popularity_asc'] = 'Popularity (Ranked Highest)';
        	$sortby['popularity_desc'] = 'Popularity (Ranked Lowest)';
        	$sortby['sales_asc'] = 'Sales (Highest to Lowest)';
        	$sortby['sales_desc'] = 'Sales (Lowest to Highest)';
        	$sortby['date_desc'] = 'Date (Newest to Oldest)';
        	$sortby['date_asc'] = 'Date (Oldest to Newest)';
        	$sortby['title_asc'] = 'Alphabetically (A-Z)';
        	$sortby['title_desc'] = 'Reverse-Alphabetically (Z-A)';
    
        	return $sortby;
        }	
    
        add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');

    As the WooCommerce support page (http://docs.woothemes.com/document/custom-sorting-options-ascdesc/) for this only has three examples on how to do this, I was wondering how would I be able to achieve the other settings for the desired effect, as some of the options I've placed in my functions.php file aren't currently working and my sorting filter now looks like:

    http://i.stack.imgur.com/Mp0lM.jpg

    From one of the responses I received, I was told

    modify the $sortby variable in your custom_woocommerce_catalog_orderby() function to make it look the way you want

    and would kindly like some further clarification on this.

    Thank you.

  2. mdwornicki
    Member
    Posted 1 year ago #

    @user1752759B

    Your code can works fine if you change from:

    if (isset($_SESSION['orderby'])) {
       switch ($_SESSION['orderby']) :

    to:

    $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
       switch ($orderby_value) :

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.