I am trying to get the default order of Woocommerce to be order by SKU. I have changed the order in the woocommerce settings and added SKU like this:
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' );
function sv_sku_sorting_orderby( $sortby ) {
$sortby['sku'] = 'Sorteer op referentie';
// Change text above as desired; this shows in the sorting dropdown
return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );
add_filter( 'woocommerce_default_catalog_orderby_options', 'sv_sku_sorting_orderby' );
On pageload it still orders on popularity instead of on SKU. But the dropdown shows order by SKU (Sorteer op referentie)
If I go to another ordering and go back it will correctly order them with ?orderby=sku in the querystring.
Updated (Dec 2018)
You are not using the right hooks in the right way to get default ordering catalog by sku. There is also some missing needed additional code:
Code goes in function.php file of your active child theme (or active theme). Tested and works.