WooCommerce Sort Products By SKU

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About this Solution

For very rare cases, you may wish to allow customers to sort products by SKU. This use case would be useful in perhaps a B2B setting, events, or wholesaler.

				
					/**
 * Snippet Name:	WooCommerce Sort Products By SKU
 * Snippet Author:	ecommercehints.com
 */

add_filter('woocommerce_catalog_orderby', 'ecommercehints_sku_sorting_option');
function ecommercehints_sku_sorting_option($options) {
	$options['sku'] = 'Sort by SKU';
	return $options;
}

add_filter('woocommerce_get_catalog_ordering_args', 'ecommercehints_sku_sorting_option_order');
function ecommercehints_sku_sorting_option_order($args) {
	if( isset($_GET['orderby']) && 'sku' === $_GET['orderby']) {
		$args['meta_key'] = '_sku';
		$args['orderby'] = array( 'meta_value' => 'ASC' ); // Ascending Order (A-Z). Change 'ASC' to 'DESC' for Descending Order (Z-A)
	}
	return $args;
}
				
			
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

2 Responses

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

Your email address will not be published. Required fields are marked *

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers