Currently, products in the catalog are sorted by ascending price by default, but as a result, products with missing or zero prices are displayed first. How can we make sure that such products are positioned at the end of the list without changing the sorting method?
I add this code but, not work in woodmart theme
add_filter(‘posts_clauses’, ‘custom_orderby_price_with_zero_last_woodmart’, 10, 2);
function custom_orderby_price_with_zero_last_woodmart($clauses, $query) {
global $wpdb;
// Проверяем, что это основной запрос WooCommerce и страница магазина или категории
if (!is_admin() && $query->is_main_query() && (is_shop() || is_product_category()) && isset($query->query_vars[‘orderby’]) && $query->query_vars[‘orderby’] === ‘price’) {
// Обеспечиваем, что все условия сортировки будут корректно применены
$clauses[‘join’] .= ” LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = ‘_price’)”;
$clauses[‘orderby’] = “IF({$wpdb->postmeta}.meta_value IS NULL OR {$wpdb->postmeta}.meta_value = ”, 1, 0), {$wpdb->postmeta}.meta_value+0 ASC”;
}
return $clauses;
}