Community Documentation

Converting currency

Last updated February 8, 2012. Created by escuriola on November 27, 2008.
Edited by xjm, charlieman. Log in to edit this page.

I used this snippet with the currency api and the Money CCK modules.

In this code, I have an initial amount of money ($total_amount), may be in euros or in other currency ($currency field), that is the money expended by one of our distributor (both fields are provided by money cck module.). We have to evaluated the project of our distributor and check if we return the 50%, 25% or 0%, depending of several factors. This is the $result field.

In the step 2, I calculated the import to return according to our evaluation.

And finally, in the 3rd step , we write the $node_field[0]['value'] with the import to return, but first we check if the currency if in euros and if not, we convert in order to present this amount in €, all this with the help of the currency_api_convert function.

The $currency field stores the currency ISO, the same that uses the currency api to convert the money.

<?php
// Step 1
$total_amount = $node->field_coop_total[0]['amount'];
$result = $node->field_coop_result[0]['value'];
$currency = $node->field_coop_total[0]['currency'];

// Step 2
$total_result = number_format($total_amount*$result/100 ,2);

// Step 3

if ($currency == 'EUR') {

 
$node_field[0]['value'] = $total_result.' '.$currency;

}else{

 
$from = $currency;
  
$to   = 'EUR';
  
$amt  = $total_result;
  if (
module_exists('currency') && function_exists('currency_api_convert')) {
  
$ret = currency_api_convert($from, $to, $amt);
 
$node_field[0]['value'] = $ret['value'].' €';
  }else{
   
$node_field[0]['value'] = $total_result;
  }
}
?>

Comments

Hi

I need to do something similar to my website http://www.fashionjewelery.co.in

The currency to Paypal should get converted to USD

My store currency is INR. Paypal doesn't support INR. So if this INR gets converted to USD, then the correct amount would be charged. This can help accept CC or Paypal Payments. I have been searching for this to happen since months.

Please let me know if you can help.

Ragavendra BN

solution????

Hi
did u get the solution?

Reply

Hey plz tell me how to convert from INR to USD since am using paypal.... paypal takes only USD not INR so while checking out how will i convert it to USD and connect to paypal......

Possible edit needed

I tried to use this code but it didn't work for me until i changed:

if (module_exists('currency') && function_exists('currency_api_convert')) {

To:

if (module_exists('currency_api') && function_exists('currency_api_convert')) {

Hope this helps anyone else having issues.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.