How to add custom sorting in Woocommerce?

To select the default sorting you need to add two fields:
- sort by Sales - Displaying of goods with a discount price
- sort New - display of goods with ACF field (checkbox field) new value to true

The required items in the select added without problems, to sort using the code:
add_filter('woocommerce_get_catalog_ordering_args', 'wcs_get_catalog_ordering_args');
 wcs_get_catalog_ordering_args function($args)
{
 $orderby_value = isset($_GET['orderby']) ? woocommerce_clean($_GET['orderby']) : apply_filters('woocommerce_default_catalog_orderby', get_option('woocommerce_default_catalog_orderby'));
 if ('sales' === $orderby_value) {
 $args['orderby'] = 'meta_value_num';
 $args['order'] = 'DESC';
 $args['meta_key'] = '_sale_price';
 $args['meta_query'] = array(
array(
 'key' => '_sale_price',
 'value' => 0,
 'compare' => '>',
 'type' => 'NUMERIC',
)
);
}
 if ('new' === $orderby_value) {
 $args['orderby'] = 'meta_value';
 $args['order'] = 'DESC';
 $args['meta_key'] = 'new';
 $args['meta_query'] = array(
array(
 'key' => 'new',
 'value' => 1,
 'compare' => '=',
)
);
}
 return $args;
 }


When sorting New goods are sorted immediately withdrawn fields with New after the other. And it is necessary to exclude other goods in General.
Sorting by sales does not work.
How to add the sort?
June 3rd 19 at 20:25
1 answer
June 3rd 19 at 20:27
And it is necessary to exclude other goods in General.

If the goods need to be excluded, it is filtering but not sorting!
Although this code prints all the records which have a meta field on which to sort in the specified sequence. This suggests that if some of the entries will have the specified meta field (note that the phrase "will not" indicates the absence of such a meta field, not the empty value), this entry in the sample will not fall and will not be displayed.

Find more questions by tags WooCommerce