PluginUs.Net - Business Tools for WooCommerce and WordPress

[realize your idea - make your dreams come true]

Support Forum

You need to log-in to create request (topic) to the support

Function to convert value from one currency to another currency

The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday. If you have problems with registration ask help on contact us page please
If you not got email within 24~36 business hours, firstly check your spam box, and if no any email from the support there - back to the forum and read answer here. DO NOT ANSWER ON EMAILS [noreply@pluginus.net] FROM THE FORUM!! Emails are just for your info, all answers should be published only here.
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday.

In a certain scenario i needed to convert the value from a currency not default or current to another currency not default or current.

I've created the following function:

function woocs_convert_from_currency_to_currency($value, $from_currency, $to_currency) {
    global $WOOCS;
    if ($from_currency == $to_currency) {
        return $value;
    }
    $currencies = $WOOCS->get_currencies();
    $def_currency = $WOOCS->default_currency;
    if ($from_currency == $def_currency) {
        return floatval($value) * floatval($currencies[$to_currency]["rate"]);
    } else if ($to_currency == $def_currency) {
        return floatval($value) / floatval($currencies[$from_currency]["rate"]);
    } else {
        return (floatval($value) / floatval($currencies[$from_currency]["rate"])) * floatval($currencies[$to_currency]["rate"]);
    }
}

Can someone verify if the logic is accurate? And if it is, i think it could be a nice addition to the $WOOCS api;

Hello

The function should include a check for the existence of a currency

I will forward your idea to the developers