Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sort WooCommerce Products by SKU
<?php // ONLY COPY THIS LINE IF NEEDED!
/**
* Adds the ability to sort products in the shop based on the SKU
* Can be combined with tips here to display the SKU on the shop page: https://www.skyverge.com/blog/add-information-to-woocommerce-shop-page/
*
* @param array $args the sorting args
* @return array updated args
*/
function sv_add_sku_sorting( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'sku' == $orderby_value ) {
$args['orderby'] = 'meta_value';
$args['order'] = 'asc'; // lists SKUs alphabetically 0-9, a-z; change to desc for reverse alphabetical
$args['meta_key'] = '_sku';
}
return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'sv_add_sku_sorting' );
/**
* Add the option to the orderby dropdown.
*
* @param array $sortby the sortby options
* @return array updated sortby
*/
function sv_sku_sorting_orderby( $sortby ) {
// Change text above as desired; this shows in the sorting dropdown
$sortby['sku'] = __( 'Sort by SKU', 'textdomain' );
return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );
add_filter( 'woocommerce_default_catalog_orderby_options', 'sv_sku_sorting_orderby' );
@pakistanigeek

This comment has been minimized.

Copy link

@pakistanigeek pakistanigeek commented Feb 25, 2016

Thank you, you saved my time 👍

@EvansJunatan

This comment has been minimized.

Copy link

@EvansJunatan EvansJunatan commented Jul 29, 2016

How to make sort by sku as default? Thankyou very much.

@kaydeeweb

This comment has been minimized.

Copy link

@kaydeeweb kaydeeweb commented Oct 24, 2017

Awesome thanks so much. Did exactly what I needed it to.

@programmer1402

This comment has been minimized.

Copy link

@programmer1402 programmer1402 commented Apr 12, 2019

Thank you, it solved my problem :)

@eludlow

This comment has been minimized.

Copy link

@eludlow eludlow commented Jun 3, 2019

Really really useful, thank you! Is there an easy way of forcing WC to use this new SKU sort as the default sorting option?

@bekarice

This comment has been minimized.

Copy link
Owner Author

@bekarice bekarice commented Jun 5, 2019

You can choose it as the default in your settings when this snippet is applied

@eludlow

This comment has been minimized.

Copy link

@eludlow eludlow commented Jun 6, 2019

So you can! I had totally forgotten the "default product sorting" was under Appearance -> Customize now.

Many, many thanks - great snippet. Virtual beer heading your way!

@jorgeinturias

This comment has been minimized.

Copy link

@jorgeinturias jorgeinturias commented Sep 2, 2019

Maybe you can help us, this code works perfectly, the problem is with the numbers example:

A-01
A-02
A-03
....
A-09
A-10
A-100
A-11
A-12
.....

any suggestion? thanks in advance

@bielcarreras

This comment has been minimized.

Copy link

@bielcarreras bielcarreras commented Jun 10, 2020

I'm having the same problem .. any guess yet? thanks

@bekarice

This comment has been minimized.

Copy link
Owner Author

@bekarice bekarice commented Jun 10, 2020

natural sorting isn't possible without more extensive customization of WordPress, you can read more here. Not something you could do with these filters since they use WP_Query.

@ArneSaknussemm

This comment has been minimized.

Copy link

@ArneSaknussemm ArneSaknussemm commented Jul 9, 2020

Thanks a lot for this, i thought it would be harder to do

@butifarra

This comment has been minimized.

Copy link

@butifarra butifarra commented Oct 5, 2020

Great code, thank you very much. I want to order a machinery catalog by brand and between each brand by year, and between year by price. That is, for example:
John Deere machines go first. Among all the John Deere, the order must be by year descending, and when the year coincides, the more expensive go first. How can I achieve this? I read a lot of material but nothing helpful. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.