FOX - Currency Switcher for WooCommerce

Comment ajouter un agrégateur de taux de change personnalisé

Si aucun des agrégateurs intégrés ne vous convient — votre banque publie ses propres taux, ou l'entreprise utilise un taux interne — FOX prend une source de taux de change de votre propre.

À partir de WOOCS version 2.3.1/1.3.1, cela est possible avec les 2 hooks suivants : woocs_announce_aggregator et woocs_add_aggregator_processor dans le fichier functions.php du thème WordPress actuel. Exemple :
add_action('woocs_announce_aggregator', function($aggregators) {
    $aggregators['hello_world'] = 'My own aggregator';
    return $aggregators;
});

add_action('woocs_add_aggregator_processor', function($aggregator_key, $currency_name) {
    global $WOOCS;
    $request = [];

    //ratesapi.io as an example
    //see more examples in file \classes\woocs.php in public function get_rate()
    if ($aggregator_key === 'hello_world') {
        $query_url = 'https://api.ratesapi.io/api/latest?base=' . $WOOCS->default_currency . '&symbols=' . $currency_name;
        
        if (function_exists('curl_init')) {
            $res = $WOOCS->file_get_contents_curl($query_url);
        } else {
            $res = file_get_contents($query_url);
        }

        $data = json_decode($res, true);
        $request = isset($data['rates'][$currency_name]) ? $data['rates'][$currency_name] : 0;

        if (!$request) {
            $request = sprintf("no data for %s", $currency_name);
        }
    }

    return $request;
}, 10, 2);