• Resolved talha6248

    (@talha6248)


    I have been using give and found it very useful but the thing is our old site has the feature of multiple currency feature for user to select. Now i know the give currency switcher is actively under development and all the best for it but we currently dont want to change the currency we want a simple converter, a simple field above the donation amount for a currency converter achieved through some currency converter plugin or api from user selected currency to our primary currency USD fixed so user will be known that what i donate is equals to that USD amount . By placing some code snippets or functions can we achieve that and how?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Devin Walker

    (@dlocc)

    Interesting question @talha6248 – I totally get what you’re asking. Unfortunately, nothing like this exists. Something like this wouldn’t be too hard to put together. This sounds like a functionality we can incorporate in our soon to be released Currency Switcher add-on. Are you looking to pull the exchange rates via an API or set the manually?

    Thread Starter talha6248

    (@talha6248)

    Hi Devin!
    Yes we need to pull the exchange rates through “Open Exchange Rate API”.

    Plugin Author Matt Cromwell

    (@webdevmattcrom)

    @talha6248 — reach out via our contact form: https://givewp.com/contact-us and ask for me and refer to this support ticket URL. We can chat about it in detail over there.

    Thanks!

    @talha6248 This may be of use to you, I did something similar. In my case I wanted to show the donation goals and current donation level in local currencies for people visiting our other country sites.

    I wrote a small function to query google API and get the exchange rates. One thing to note though is to better do this in a cron once a day or so and save it locally in the DB. Querying API for every conversion slowed down the site drastically, so I don’t recommend that.

    Alternatively, if you could do it over javascript, so it runs in the users browser only, then it should be fine to use API. I assume that’s what you want to do anyways, as it sounds you just want to convert some input field.

    Anyways, for my use, since I just wanted 2 additional currencies and processing done server side, I just misused the options table to store currencies ??

    You could do something like this:

    
    function tow_cron_activation() {
            if (! wp_next_scheduled ( 'get_currency_exchange_rate' )) {
                    wp_schedule_event(time(), 'daily', 'get_currency_exchange_rate');
            }
    }
    register_activation_hook(__FILE__, 'tow_cron_activation');
    
    function tow_cron_deactivation() {
            wp_clear_scheduled_hook('get_currency_exchange_rate' );
    }
    register_deactivation_hook(__FILE__, 'tow_cron_deactivation');
    
    function tow_exchange_rate() {
            $prev_rate=get_option('tow_donation_exchange_rate');
            $rate=array('timestamp' => time() );
    
            foreach( array("AUD", "INR") as $currCode) {
                    $get = file_get_contents("https://finance.google.com/finance/converter?a=1&from=USD&to=".urlencode($currCode));
                    $get = explode("<span class=bld>",$get);
                    $get = explode("</span>",$get[1]);
                    $new_rate = preg_replace("/[^0-9\.]/", null, $get[0]);
                    if (! $new_rate > 0) {
                            $new_rate = $prev_rate['INR'];
                    }
                    $rate[$currCode]=$new_rate;
            }
           update_option('tow_donation_exchange_rate', $rate);
    
    }
    add_action('get_currency_exchange_rate', 'tow_exchange_rate');
    

    Then you just would need to do something like get_option(‘tow_donation_exchange_rate’); to get the rates.

    It’s a quick hack, but does the job.

    Plugin Author Matt Cromwell

    (@webdevmattcrom)

    Hi @talha6248 and @pineapplejuice
    We just released our Currency Switcher Addon today. Check it out!
    https://givewp.com/addons/currency-switcher/

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Donation currency converter field’ is closed to new replies.