Problem: The client reported inconsistencies in product sorting across different language pages on their WooCommerce site, even after previous code adjustments. Solution: We provided custom code to ensure consistent sorting by date, SKU, price, and popularity across all language versions of the site. Here are the steps we recommended:
//Use the below snippet when sorting by price and popularity, so that SKU is used as the secondary sort option:
add_filter('posts_clauses', 'custom_orderby_with_secondary_sku', 20, 2);
function custom_orderby_with_secondary_sku($clauses, $query) {
if (!is_admin() && $query->is_main_query() && is_woocommerce() && isset($_GET['orderby'])) {
$orderby = $_GET['orderby'];
// Define primary column and direction based on ordering
switch ($orderby) {
case 'price':
$primary_column = 'wc_product_meta_lookup.min_price';
$primary_direction = 'ASC';
break;
case 'price-desc':
$primary_column = 'wc_product_meta_lookup.max_price';
$primary_direction = 'DESC';
break;
case 'popularity':
$primary_column = 'wc_product_meta_lookup.total_sales';
$primary_direction = 'DESC';
break;
default:
return $clauses; // Not a target orderby value, leave alone
}
// Set ORDER BY with SKU as secondary sort
$clauses['orderby'] = "
{$primary_column} {$primary_direction},
wc_product_meta_lookup.sku ASC
";
}
return $clauses;
}
If this solution does not resolve your issue or seems irrelevant due to updates or specific circumstances, we recommend opening a new support ticket. Additionally, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. For further assistance, please visit our support forum at WPML Support Forum.