-
-
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' ); |
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
.
Thanks a lot for this, i thought it would be harder to do
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.
Thank you, this works for me.
I am unable to find the string in WPML for translating it. Any idea? Thank you!
How can I enable this in Woocommerce REST API too ?
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.
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 💥
I'm having the same problem .. any guess yet? thanks