Jump to content

formatCurrency


Recommended Posts

Hello,

 

I'm using the code below for multi currency:

 

$currencyData = $_SESSION["currency"];
$license["price"] = formatCurrency($license["price"],$currencyData);

 

The code works in the meaning that changed currency Prefix and currency Suffix but don't updates the number.

The price is MySQL format:

`price` DECIMAL(10,2) NOT NULL DEFAULT '0.00',

and in database appears correct.

 

Any idea on what's going wrong?

 

Thank you

Chris

 

- - - Updated - - -

 

......Addition......

I've a black thought that this function works only for whmcs products and the only work that it does is to change prefix, suffix and getting the price from the product database for that currency.

Link to comment
Share on other sites

then perhaps the array doesn't contain both USD and EUR pricing, only one.

 

Which array? When someone is saying pass a currency code along with a value, the function must works. Later on today I'll code my own functions and should works with any given value pair (amount, currency).

I wasted already valuable time trying to figure out how the "blocked" WHMCS functions are working. As you seen in the link before, someone else had the same problem 3 years ago.

Link to comment
Share on other sites

I was thinking specifically of the $products array...

 

if you had a product that was priced in 2 currencies, the array is recalculated during a change in currency from the selector... so when currency is USD, it's using the USD prices; when you change it to EUR, it's using EUR pricing.

Link to comment
Share on other sites

I was thinking specifically of the $products array...

 

if you had a product that was priced in 2 currencies, the array is recalculated during a change in currency from the selector... so when currency is USD, it's using the USD prices; when you change it to EUR, it's using EUR pricing.

 

No, I don't have 2 prices. And the documentation does not says anything about having array with 2 or more prices. Anyway, as I said, I already waste lot of time for tests and posts. I'll make my own function and much more flexible:

getCurrency($amount,$currency,$prefix,$suffix)

 

With the last 2 parammeters I can show/hide prefix/suffix depending to my needs.

Edited by ChrisTERiS
Typo
Link to comment
Share on other sites

No, I don't have 2 prices. And the documentation does not says anything about having array with 2 or more prices. Anyway, as I said, I already waste lot of time for tests and posts. I'll make my own function and much more flexible:

getCurrency($amount,$currency,$prefix,$suffix)

With the last 2 parameters I can show/hide prefix/suffix depending to my needs.

if you're in the cart, you'll already have access to $currency which has all that information already.

and I see what you mean about it not having two prices - your products have a visual price, and not multiple prices in the usual way that a normal product would .

Link to comment
Share on other sites

https://www.teriakis.com/cart.php

 

Select USD and then click or any product.

The function works fine with the function using mysqli . Let's see how it will be possible to work (or not) in the homepage and the hook.

 

- - - Updated - - -

 

For those who want to use this function:

 

//  #######################################################################
//  ############################  Exchange Currency  ######################
//  #######################################################################
function getExchange($amount,$currency,$prefix,$suffix)
{
   global $db, $CFG, $LANG;

   $sql = mysqli_query($db,"SELECT * FROM tblcurrencies WHERE id=$currency ORDER BY id ASC LIMIT 1");
   $rlt = mysqli_fetch_assoc($sql);
   // New Amount
   $_amount = $amount * $rlt["rate"];
   // Prefix
   if ($prefix == 1)
   {
       $_prefix = $rlt["prefix"];
   } else {
       $_prefix = '';
   }
   // Suffix
   if ($suffix == 1)
   {
       $_suffix = $rlt["suffix"];
   } else {
       $_suffix = '';
   }    
   // Format
   if ($rlt["format"] == 1)
   {
       $_format = '1234.56';
       $thousands_seperator = '';
       $decimals_seperator = '.';
       $decimals = 2;        
   }
   if ($rlt["format"] == 2)
   {
       $_format = '1,234.56';
       $thousands_seperator = ',';
       $decimals_seperator = '.';
       $decimals = 2;        
   }
   if ($rlt["format"] == 3)
   {
       $_format = '1.234,56';
       $thousands_seperator = '.';
       $decimals_seperator = ',';
       $decimals = 2;        
   }
   if ($rlt["format"] == 4)
   {
       $_format = '1,234';
       $thousands_seperator = ',';
       $decimals_seperator = '';
       $decimals = 0;        
   }    

   return $_prefix.number_format($_amount, $decimals, ''.$decimals_seperator.'', ''.$thousands_seperator.'').' '.$_suffix;
}

 

You can get the $currency value from $_SESSION["currency"]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated