FOX - Currency Switcher for WooCommerce

woocs_add_aggregator_processor

The woocs_add_aggregator_processor filter supplies the code that fetches rates for a custom aggregator. Available from v.2.3.1 / 1.3.1. It works together with woocs_announce_aggregator: that one puts your source in the settings list, this one runs when the shop owner has selected it and FOX asks for a rate.
add_filter('woocs_add_aggregator_processor', function ($rate, $aggregator, $currency) {
    if ($aggregator !== 'my_bank') {
        return $rate;   // not ours — leave it alone
    }

    $response = wp_remote_get('https://example.com/api/rate/' . $currency);
    if (is_wp_error($response)) {
        return $rate;   // on failure keep the previous rate
    }

    $data = json_decode(wp_remote_retrieve_body($response), true);

    return isset($data['rate']) ? floatval($data['rate']) : $rate;
}, 10, 3);
Parameters:
  • $rate - the rate resolved so far
  • $aggregator (string) - the id selected in the settings
  • $currency (string) - the currency code being resolved
Returns:
  • the rate to store
Important: always check the aggregator id first and return the incoming value unchanged when it is not yours, or you will overwrite the rates of every other source. And return the previous rate on a failed request rather than zero — a zero rate makes every price in that currency free.