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'] = 'DESC' === $order ? 'DESC' : 'ASC';
$args['meta_key'] = '';
switch ($orderby) {
case 'rand':
$args['orderby'] = 'rand';
break;
case 'date':
$args['orderby'] = 'date ID';
$args['order'] = 'ASC' === $order ? 'ASC' : 'DESC';
break;
case 'price':
$args['orderby'] = "meta_value_num ID";
$args['order'] = 'DESC' === $order ? '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':
$args['meta_key'] = '_wc_average_rating';
$args['orderby'] = array('meta_value_num' => 'DESC', 'ID' => 'ASC');
break;
case 'title':
$args['orderby'] = 'title';
$args['order'] = 'DESC' === $order ? 'DESC' : 'ASC';
break;
}
return apply_filters('woocommerce_get_catalog_ordering_args', $args);
}