Need an advice how to get selected currency with ajax call in Woocommerce

[ad_1]

I created custom `Woocommerce` theme (by code, not builder) and it works fine. I added `WPML` plugin with multi currency option. I added additional currencies which changes depending on selected language. Everywhere products show correct currency depending on selected language. But I face with problem in my custom search. When I do search by string I able to get `get_price_html()` only in default currency (EUR). I’m pretty new with `Wordpress` (not programming) so maybe I missing something why I am not able to get `get_price_html()` with selected current in `ajax` call?

function do_search()
{
check_ajax_referer(‘main_nonce’, ‘security’);

$search_query = sanitize_text_field($_POST[‘search_query’]);
$reults = array();

$args = array(
‘s’ => $search_query,
‘post_type’ => array(‘post’, ‘product’),
‘posts_per_page’ => 10,
);

$query = new WP_Query($args);

// Check is query not a empty string
if ($search_query === ”) {
wp_send_json(array(
‘status’ => ‘success’,
‘results’ => $reults,
));
return;
}

if ($query->have_posts()) {

// Get all rezults data
while ($query->have_posts()) {
$query->the_post();

$product = wc_get_product($query->post->ID);
$price_html = $product->get_price_html();

array_push($reults, array(
‘title’ => get_the_title(),
‘price’ => $price_html,
‘permalink’ => get_permalink(),
‘image’ => get_the_post_thumbnail_url(get_the_ID(), ‘thumbnail’),
));
}

// Return results data
wp_send_json(array(
‘status’ => ‘success’,
‘results’ => $reults,
));
} else {
// Return no results message
wp_send_json(array(
‘status’ => ‘success’,
‘results’ => $reults,
));
}

wp_reset_postdata();
}
add_action(‘wp_ajax_do_search’, ‘do_search’);
add_action(‘wp_ajax_nopriv_do_search’, ‘do_search’);

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer