Answer the question
In order to leave comments, you need to log in
How to add custom sorting in Woocommerce?
You need to add two fields to the default sort select:
- sorting by Sales - Displaying products with a discount price
- sorting New - displaying products that have an ACF field (checkbox field) new with a value of true
I added the necessary items in the select without problems, I use it for sorting the code:
add_filter('woocommerce_get_catalog_ordering_args', 'wcs_get_catalog_ordering_args');
function wcs_get_catalog_ordering_args($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;
}
Answer the question
In order to leave comments, you need to log in
And you need to exclude other products altogether.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question