-
-
Save bekarice/1883b7e678ec89cc8f4d to your computer and use it in GitHub Desktop.
<?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' ); |
Thanks a ton
It works! Thank you, you saved my time!
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';
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
Great code! Thanks :)
This code worked perfect! And setting the default order by Appearance Settings did the trick! Thanks!
Awesome! Thnx!
Thank you 💥
It works great, thank you! Any way to add both alphabetical and reverse alphabetical without getting error on frontpage?
I just:
... but that's not enough since I still get error on frontpage.