我正在尝试从最大变化价格中对 WooCommerce 可变产品进行排序(我的网站包含简单和可变产品)。
下面我的自定义查询是按价格对产品进行排序,但问题是它从可变产品中获取最小变化价格。有什么方法可以按最大变化价格对它们进行排序吗?
function products_load_more() {
$size = $_POST['size'];
$count = $_POST['count'];
$strength = $_POST['strength'];
$country = $_POST['country'];
$taxonomy_id = $_POST['taxonomy_id'];
$category = $_POST['category'];
$amount1 = (int) $_POST['amount1'];
$amount2 = (int) $_POST['amount2'];
$sorting = $_POST['sorting'];
$range_slider_array = array(0=>0,1=>100,2=>200,3=>300,4=>400,5=>500,6=>600,7=>700,8=>800,9=>900,10=>1000,11=>1500,12=>2000,13=>2500,14=>3000,15=>3500,16=>4000,17=>4500,18=>5000);
if($sorting == "h_t_l"){
$order = "DESC";
}
else{
$order = "ASC";
}
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'orderby' => 'date',
'order' => 'DESC',
'paged' => $_POST['paged'],
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $taxonomy_id
) )
);
// price
if(isset($_POST['amount1']) && isset($_POST['amount2'])){
if(trim($amount1) != "" && trim($amount2) != ""){
if($range_slider_array[$amount2] == 5000){
$args['meta_query'][] = array(
'key' => '_price',
'value' => $range_slider_array[$amount1],
'compare' => '>=',
'type' => 'NUMERIC'
);
}
else{
$args['meta_query'][] = array(
'key' => '_price',
'value' => array($range_slider_array[$amount1], $range_slider_array[$amount2]),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
);
}
}
}
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
$args['meta_key'] = '_price';
$ajaxposts = new WP_Query($args);
$count = $ajaxposts->found_posts;
$response = '';
if($ajaxposts->have_posts()) {
while($ajaxposts->have_posts()) : $ajaxposts->the_post();
include get_stylesheet_directory()."/inc/product.php";
endwhile;
} else {
$response = 0;
}
echo $response;
exit;
}
add_action('wp_ajax_products_load_more', 'products_load_more');
add_action('wp_ajax_nopriv_products_load_more', 'products_load_more');
function sort_products_by_max_variation_price($query) {
if (is_admin() || !$query->is_main_query() || !is_shop()) {
return;
}
global $wpdb;
$query->set('meta_query', array(
'relation' => 'OR',
array(
'key' => '_min_variation_price',
'compare' => 'EXISTS',
),
array(
'key' => '_price',
'compare' => 'EXISTS',
),
));
$query->set('orderby', 'meta_value_num');
$query->set('order', 'DESC');
$query->set('meta_key', 'max_price');
// Add a join clause to include maximum variation prices
add_filter('posts_join', function($join) use ($wpdb) {
$join .= " LEFT JOIN (
SELECT post_parent, MAX(meta_value+0) as max_price
FROM {$wpdb->postmeta}
WHERE meta_key = '_price'
GROUP BY post_parent
) as max_price_table ON {$wpdb->posts}.ID = max_price_table.post_parent";
return $join;
});
// Add a select clause to include maximum variation prices in the query
add_filter('posts_fields', function($fields) use ($wpdb) {
$fields .= ", max_price_table.max_price as max_price";
return $fields;
});
// Add an orderby clause to sort by maximum variation prices
add_filter('posts_orderby', function($orderby) use ($wpdb) {
$orderby = "max_price DESC, {$wpdb->posts}.post_date DESC";
return $orderby;
});
}
add_action('pre_get_posts', 'sort_products_by_max_variation_price');