Content of the article
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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; } )); }); |
