Skip to content

Instantly share code, notes, and snippets.

@bekarice
Last active April 8, 2024 17:56
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • 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' );
@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