Skip Navigation

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.

This topic contains 0 replies, has 1 voice.

Last updated by anastasiaM 4 months, 2 weeks ago.

Author Posts
December 21, 2023 at 2:20 pm #15111095

anastasiaM

//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?

December 21, 2023 at 2:57 pm #15111563

anastasiaM

add_filter( 'jet-smart-filters/query/final-query', function( $query ) {

if ( ! isset( $query['meta_query'] ) ) {
return $query;
}

foreach ( $query['meta_query'] as $index => $item ) {

if ( false !== strpos( $query['meta_query'][$index]['key'] ?? '', 'woo__on_sale' ) ) {

unset( $query['meta_query'][ $index ] );

// 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;
}

if ( empty( $query['post__in'] ) ) {
$query['post__in'] = array( 0 );
}
}
}

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.