//Filter on sale when language is french
add_filter('jet-smart-filters/query/final-query', function ($query) {
if (!isset($query['meta_query'])) {
return $query;
}
foreach ($query['meta_query'] as $index => $item) {
// Replace 'woo__on_sale' with the actual name of your meta key
if (false !== strpos($item['key'] ?? '', 'woo__on_sale')) {
// Remove the 'woo__on_sale' meta query
unset($query['meta_query'][$index]);
// Get on-sale product IDs
$on_sale_products = wc_get_product_ids_on_sale();
// If on-sale products are found
if (!empty($on_sale_products)) {
// Filter products based on the wpml_language_code meta key for French
$wpml_language_code = 'fr';
$query['meta_query'][] = array(
'key' => 'wpml_language_code',
'value' => $wpml_language_code,
'compare' => '=',
);
}
// Set post__in to on-sale products
$query['post__in'] = $on_sale_products;
// If no on-sale products, set post__in to an array with 0
if (empty($query['post__in'])) {
$query['post__in'] = array(0);
}
}
}
return $query;
});
this code works on the original language. But on the translations it gives zero products. Why?
// Get on-sale product IDs
$on_sale_products = wc_get_product_ids_on_sale();
// Check if it's a WPML environment
if ( function_exists( 'icl_object_id' ) ) {
// Get the current language
$current_language = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : '';
// Translate on-sale product IDs to the current language
foreach ( $on_sale_products as $product_id ) {
$translated_product_id = icl_object_id( $product_id, 'product', true, $current_language );
if ( $translated_product_id ) {
$translated_on_sale_products[] = $translated_product_id;
}
}
// If no translated on-sale products, set post__in to an array with 0
if ( empty( $translated_on_sale_products ) ) {
$translated_on_sale_products = array( 0 );
}
// Set post__in to translated on-sale products
$query['post__in'] = $translated_on_sale_products;
} else {
// If not using WPML, use on-sale products directly
$query['post__in'] = $on_sale_products;
}
return $query;
});
This code worked..after one week of trial and error
anastasiaM confirmed that the issue was resolved on 2023-12-21 14:57:17.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.