1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Woocommerce Paypal Currency Converter

Discussion in 'PHP & Perl' started by Conor, Mar 19, 2014.

  1. Conor

    Conor Jr. VIP Jr. VIP

    Joined:
    Nov 7, 2012
    Messages:
    3,363
    Likes Received:
    5,424
    Gender:
    Male
    Location:
    South Africa
    Home Page:
    I spent a long time today trying to find a working plugin to get Woocommerce to convert the product prices to USD automatically, when a user checks out with Paypal. I didn't find anything free or usable.

    So, with my newfound, although still limited PHP knowledge, and a LOT of Googling, I decided to put something together from scratch.

    Simply paste this into functions.php, and it should do its thing when you click "Checkout". It automatically gets the latest currency conversion from Google, which I think was a nice touch, though definitely the most difficult.

    Code:
    // Enable the currency in WC
    add_filter( 'woocommerce_currencies', 'add_zar_currency' );  
      
    function add_zar_currency( $currencies ) {  
     $currencies['ZAR'] = __( 'South African Rand (R)', 'woocommerce' );
     return $currencies;  
    }
    add_filter('woocommerce_currency_symbol', 'add_zar_currency_symbol', 10, 2);  
      
    function add_zar_currency_symbol( $currency_symbol, $currency ) {  
     switch( $currency ) {  
     case 'ZAR': $currency_symbol = 'R'; break;  
     }  
     return $currency_symbol;  
    }
    add_filter( 'woocommerce_paypal_supported_currencies', 'add_zar_paypal_valid_currency' ); 
    
    function add_zar_paypal_valid_currency( $currencies ) {    
         array_push ( $currencies , 'ZAR' );  
         return $currencies;    
        }
    add_filter('woocommerce_paypal_args', 'convert_zar_to_usd');
    
    // Automatic Conversion Bit
    function convert_zar_to_usd($paypal_args){
    $amount = urlencode(1);
        $from_Currency = urlencode("ZAR"); // Important. It's the currency you're converting
        $to_Currency = urlencode("USD"); // Also important, this needs to be a Paypal supported currency, or it won't work.
    
    
        $url = "http://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency";
    
    
        $ch = curl_init();
        $timeout = 0;
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    
    
        curl_setopt ($ch, CURLOPT_USERAGENT,
                     "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $rawdata = curl_exec($ch);
        curl_close($ch);
        $data = explode('bld>', $rawdata);
        $data = explode($to_Currency, $data[1]);
    
    
        $amnt = round($data[0], 3);
      
        if ( $paypal_args['currency_code'] == 'ZAR'){  
            $convert_rate = $amnt;
            $paypal_args['currency_code'] = 'USD';
            $i = 1;  
    
    
    // Without the IF statements here, the thing gives totally wrong figures. 
            while (isset($paypal_args['amount_' . $i])) {
    if ($amnt < 1) {  
                $paypal_args['amount_' . $i] = round($paypal_args['amount_' . $i] * $convert_rate, 2);
    }
    elseif ($amnt > 1) {  
                $paypal_args['amount_' . $i] = round($paypal_args['amount_' . $i] / $convert_rate, 2);
    }   
                ++$i;
            }  
      
        }  
    return $paypal_args;  
    }
    
    NOTE: The currency I've used is ZAR, because I'm in South Africa. But if you want to change it to your local currency, simply replace "ZAR" with your local currency in Notepad, with "Match Case" ticked. Also, remember to change line 5 and line 12 to your currency symbol of choice.

    I'll try put together a frontend interface to do all this automatically, perhaps sometime in the future.

    Thoughts? Critiques? It's a messy, rough script, but it works for me at least.
     
    • Thanks Thanks x 5
  2. kbklash

    kbklash Senior Member

    Joined:
    Jun 2, 2012
    Messages:
    1,140
    Likes Received:
    449
    Location:
    in my BMW to my BHW
    Home Page:
    Nice effort Conor.I will forward this tweak to my partner.Thanks
     
    • Thanks Thanks x 1
  3. Riders On The Storm

    Riders On The Storm Senior Member

    Joined:
    Feb 27, 2012
    Messages:
    1,150
    Likes Received:
    489
    Thank for the hard work. It may come handy for my woocommerce sites.
     
    • Thanks Thanks x 1
  4. swifty123

    swifty123 Newbie

    Joined:
    Feb 15, 2013
    Messages:
    7
    Likes Received:
    0
    Thanks for your effort!
     
  5. citizenx28

    citizenx28 Regular Member

    Joined:
    Jan 24, 2009
    Messages:
    395
    Likes Received:
    233
    Awesome work out
     
  6. sumalatha

    sumalatha Newbie

    Joined:
    Oct 20, 2014
    Messages:
    3
    Likes Received:
    0
    Hi,
    The code working perfectly. It is converting ZAR To USD. But the thing is its not converting the R450 t0 Some USD price. Its just converting R450 to $450. Please help me to convert to exact dollar amount of my ZAR price.

    Thanks,
    Suma.
     
  7. Conor

    Conor Jr. VIP Jr. VIP

    Joined:
    Nov 7, 2012
    Messages:
    3,363
    Likes Received:
    5,424
    Gender:
    Male
    Location:
    South Africa
    Home Page:
    That's strange. Are you using PayPal as your primary gateway? PM me your site once you get to 15 posts and I can take a look for you.
     
  8. sumalatha

    sumalatha Newbie

    Joined:
    Oct 20, 2014
    Messages:
    3
    Likes Received:
    0
    Yes Paypal is the primary gateway. I din't understand about 15 posts. whta's that? I need to resolve it soon as its very urgent to me. Can You people help me?

    Regards,
    Suma.
     
  9. sumalatha

    sumalatha Newbie

    Joined:
    Oct 20, 2014
    Messages:
    3
    Likes Received:
    0
    Yes Paypal is the primary gateway. I din't understand about 15 posts. whta's that? I need to resolve it soon as its very urgent to me. Can You people help me?

    Regards,
    Suma.
     
  10. member8200

    member8200 Regular Member

    Joined:
    Aug 9, 2014
    Messages:
    469
    Likes Received:
    33
    Hi! thank you so much for sharing this, appreciate your effort. This might come in handy to my next projects.