Home›Support›English Support›[Resolved] Inconsistent sorting between Chinese and English when choosing to sort by price
[Resolved] Inconsistent sorting between Chinese and English when choosing to sort by price
This thread is resolved. Here is a description of the problem and solution.
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.
This is the technical support forum for WPML - the multilingual WordPress plugin.
Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.
I had added the following code in functions.php, and it is showing the expected results after that. Please check it and let us know your feedback.
function custom_woocommerce_meta_query( $q ) {
// Only run on main WooCommerce shop or product category/taxonomy pages
if ( ! is_admin() && is_woocommerce() && is_main_query() && isset($_GET['orderby']) ) {
// Get current meta query
$meta_query = $q->get( 'meta_query' );
// Add your custom meta query (example: show only products with custom_field = 'yes')
$meta_query[] = array(
'meta_key' => '_sku',
'meta_value' => 'ASC',
);
// Set the modified meta query
$q->set( 'meta_query', $meta_query );
}
}
add_action( 'woocommerce_product_query', 'custom_woocommerce_meta_query' );
Hello, even after adding the code you added, there are still some inconsistencies in the sorting; Here is one of the links:
hidden link
hidden link
Could you please update it again? I need the sorting of both language pages to be consistent regardless of the sorting method or category on the store page. Thank you
We are still exploring a possible workaround to achieve this, but there are some limitations. So, shared the details with our team for a detailed review. We will get back to you as soon as possible. Please wait.
We are getting the expected result after testing the following code. I have already updated it on your staging site. Please clear all types of caches and check if everything is now working as expected.
//The following code is used when no sorting option is selected — it defaults to sorting by date and then SKU.
add_action('pre_get_posts', 'custom_modify_shop_query', PHP_INT_MAX);
function custom_modify_shop_query($query) {
// Only intervene on front-end shop or product category/tag archives
if ( !isset($_GET['orderby']) && !is_admin() && $query->is_main_query() && ( is_shop() || is_product_category() || is_product_tag() ) ) {
// Set meta_key so SKU ordering works
$query->set('meta_key', '_sku');
// Set orderby to array: first by date, then by SKU
$query->set('orderby', array(
'date' => 'DESC',
'meta_value' => 'ASC',
));
}
}
//Use the below snippet when sorting by price, so that SKU is used as the secondary sort option:
add_filter('posts_clauses', 'custom_orderby_price_then_sku', 20, 2);
function custom_orderby_price_then_sku($clauses, $query) {
if (!is_admin() && $query->is_main_query() && is_woocommerce() && isset($_GET['orderby'])) {
$orderby = $_GET['orderby'];
// Determine price column and sort direction
if ($orderby === 'price') {
$price_column = 'wc_product_meta_lookup.min_price';
$price_direction = 'ASC';
} elseif ($orderby === 'price-desc') {
$price_column = 'wc_product_meta_lookup.max_price';
$price_direction = 'DESC';
} else {
return $clauses; // Not price-based ordering, skip
}
// Use SKU from meta lookup (already joined by WC)
$clauses['orderby'] = "
{$price_column} {$price_direction},
wc_product_meta_lookup.sku ASC
";
}
return $clauses;
}
For your kind information, if you still need further assistance with this, we kindly recommend contacting a developer, as this involves custom development work. Thank you for your understanding and cooperation.
Please check the following links now. The sorting is working as expected (the secondary SKU sorting is also applied when sorting products by popularity) after updating the code as shown below.
hidden link
hidden link
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;
}