Okay so I think I’ve managed to come up with a solution.
Rather than set SKU: Descending as default I’ve used the following code to add sku as a shortcode option:
<?php
/**
* Returns an array of arguments for ordering products based on the selected values
*
* @access public
* @return array
*/
public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
// Get ordering from query string unless defined
if ( ! $orderby ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
// Get order + orderby args from string
$orderby_value = explode( '-', $orderby_value );
$orderby = esc_attr( $orderby_value[0] );
$order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
}
$orderby = strtolower( $orderby );
$order = strtoupper( $order );
$args = array();
// default - menu_order
$args['orderby'] = 'menu_order title';
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
$args['meta_key'] = '';
switch ( $orderby ) {
case 'rand' :
$args['orderby'] = 'rand';
break;
case 'date' :
$args['orderby'] = 'date';
$args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
break;
case 'price' :
$args['orderby'] = 'meta_value_num';
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
$args['meta_key'] = '_price';
break;
case 'popularity' :
$args['meta_key'] = 'total_sales';
// Sorting handled later though a hook
add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
break;
case 'rating' :
// Sorting handled later though a hook
add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
break;
case 'title' :
$args['orderby'] = 'title';
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
break;
case 'sku' :
$args['orderby'] = 'meta_value';
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
$args['meta_key'] = '_sku';
break;
}
return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
}
I copied the majority of this from woocommerce / includes / class-wc-shortcodes.php and added:
case 'sku' :
$args['orderby'] = 'meta_value';
$args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
$args['meta_key'] = '_sku';
break;
to the list.
The result being I can now add use “sku” as orderby= in product category shortcodes… sweet! Haha!
[product_category category="x-men-vol-4" per_page="99" columns="5" orderby="sku" order="desc" meta_key=""]
You need to create a new file called class-wc-query.php in your child theme:
public_html/wp-content/themes/’your child theme’/woocommerce/includes
and then paste the code in there.