Skip to content

Instantly share code, notes, and snippets.

@bekarice
Last active April 8, 2024 17:56
Show Gist options
  • Save bekarice/1883b7e678ec89cc8f4d to your computer and use it in GitHub Desktop.
Save bekarice/1883b7e678ec89cc8f4d to your computer and use it in GitHub Desktop.
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' );
@jorgeinturias
Copy link

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
Copy link

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

@bekarice
Copy link
Author

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
Copy link

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

@butifarra
Copy link

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.

@lobebe
Copy link

lobebe commented Jan 29, 2021

Thank you, this works for me.

@darreradev
Copy link

I am unable to find the string in WPML for translating it. Any idea? Thank you!

@dhirr
Copy link

dhirr commented Mar 23, 2021

How can I enable this in Woocommerce REST API too ?

@masvil
Copy link

masvil commented Jun 25, 2021

It works great, thank you! Any way to add both alphabetical and reverse alphabetical without getting error on frontpage?
I just:

  • cloned the snippet
  • changed function name
  • changed 'asc' to 'desc'
  • changed describing text for dropdown

... but that's not enough since I still get error on frontpage.

@thenibirahmed
Copy link

Thanks a ton

@facet100
Copy link

It works! Thank you, you saved my time!

@firmansyark
Copy link

firmansyark commented Nov 18, 2021

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

if you use alphabet & number in SKU, change
$args['orderby'] = 'meta_value';
to
$args['orderby'] = 'meta_value meta_value_num';

if just number, change to
$args['orderby'] = 'meta_value_num';

@MendyAdler
Copy link

Would appreciate help
I want the products listed for "order"
Will be sorted in ascending or descending order by SKU

In short all my orders will be with a list of products arranged in order of SKU

Is there a way to make this or a supplement?
Thanks in advance

@makedonkavasilevaa
Copy link

Great code! Thanks :)

@JavoEscobar
Copy link

This code worked perfect! And setting the default order by Appearance Settings did the trick! Thanks!

@treecamp
Copy link

Awesome! Thnx!

@idusortus
Copy link

Thank you 💥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment