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 commented Feb 25, 2016

Thank you, you saved my time 👍

@EvansJunatan

This comment has been minimized.

Copy link

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 commented Oct 24, 2017

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

@programmer1402

This comment has been minimized.

Copy link

programmer1402 commented Apr 12, 2019

Thank you, it solved my problem :)

@eludlow

This comment has been minimized.

Copy link

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

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.