Using REST API with woocommerce currency switcher
From plugin version 2.2.9/1.2.9 appeared possibility to use in-built REST request to get site currencies data: wp-json/woocs/v3/currency
Also WOOCS allows using its API create any custom REST requests, as an example below which allows to get all shop products prices in all its currencies (wp-json/woocs/v3/products/gbp): (add next code into the file functions.php of the current wordpress theme)
add_action('rest_api_init', function () {
//Example: wp-json/woocs/v3/products/eur/
register_rest_route('woocs/v3', '/products/(?P<currency>.+)', array(
'methods' => 'GET',
'callback' => function($request) {
global $WOOCS;
$currency = strtoupper(sanitize_key($request['currency']));
$products_ids = wc_get_products([
'return' => 'ids',
]);
//***
$res = [];
if (!empty($products_ids)) {
$_REQUEST['woocs_raw_woocommerce_price_currency'] = $currency;//MUST BE!!
foreach ($products_ids as $product_id) {
$product = wc_get_product($product_id);
if ($WOOCS->default_currency === $currency) {
$res[$product_id] = $product->get_price();
} else {
$res[$product_id] = $WOOCS->raw_woocommerce_price($product->get_price(), $product);
}
}
}
return $res;
},
'permission_callback' => '__return_true'
));
});
