Hello,
This php script converts product currency by currency
Can we do the same in javascript/jquery
function convert($cf,$urun,$secilen_paracinsi_id)
{
// if the product currency is the same as the selected currency, return the product price
if($product['para_birimi'] == $secilen_paracinsi_id)
{
return $urun['b_fiyati'];
}
// the currency_unit is different, convert the price
// if the product currency|selected currency matches a from|to entry in $cf, multiple by the factor
$index = $urun['para_birimi'] .'|'. $secilen_paracinsi_id;
if(isset($cf[$index]))
{
return $urun['b_fiyati'] * $cf[$index];
}
// if the selected currency|product currency matches a from|to entry in $cf, divide by the factor
$index = $secilen_paracinsi_id .'|'. $urun['para_birimi'];
if(isset($cf[$index]))
{
return $urun['b_fiyati'] / $cf[$index];
}
}
It would probably be better if the product/urun array input is broken into separate price and currency_unit parameters, so that the function call has - the cf array of conversion factors, the input price/b_fiyati, the input currency_unit/para_birimi, and the selected/secilen_paracinsi_id currency output.
Are you dynamically building the $cf array from a database query or is it hard-coded?
Here, the only syntax differences are in the . vs + concatenation operator and isset vs in -
<?php
// $cf is an array containing the conversion factors - the from|to values are used as an index and the cf (conversion factor) is the value
// you would query the conversion database table to build the following array of data
$cf = [];
$cf['USD|TL'] = 5.8313;
$cf['EURO|TL'] = 6.4087;
$cf['USD|EURO'] = .9030;
?>
<script>
// produce javascript object with the cf data
const cf = <?=json_encode($cf)?>
function convert(cf, price, currency_unit, selected_currency)
{
// if the product currency is the same as the selected currency, return the product price
if(currency_unit == selected_currency)
{
return price;
}
// the currency_unit is different, convert the price
// if the product currency|selected currency matches a from|to entry in cf, multiple by the factor
var index = currency_unit + '|' + selected_currency;
if(index in cf)
{
return price * cf[index];
}
// if the selected currency|product currency matches a from|to entry in cf, divide by the factor
index = selected_currency + '|' + currency_unit;
if(index in cf)
{
return price / cf[index];
}
}
// example usage
console.log(convert(cf, 10, 'USD', 'TL').toFixed(2));
console.log(convert(cf, 10, 'EURO', 'TL').toFixed(2));
</script>
I added it this way, no problem, right?
return price.toFixed(2);
function convert(cf, price, currency_unit, selected_currency)
{
// if the product currency is the same as the selected currency, return the product price
if(currency_unit == selected_currency)
{
return price.toFixed(2);
}
// the currency_unit is different, convert the price
// if the product currency|selected currency matches a from|to entry in cf, multiple by the factor
var index = currency_unit + '|' + selected_currency;
if(index in cf)
{
return (price * cf[index]).toFixed(2);
}
// if the selected currency|product currency matches a from|to entry in cf, divide by the factor
index = selected_currency + '|' + currency_unit;
if(index in cf)
{
return (price / cf[index]).toFixed(2);
}
}
When the page is loaded in edit mode I’m getting it inside the variable like below
Saved data from database
price = ‘250’;
currency_unit = ‘USD’;
“selected_currency” returns the selected currency to convert with the select option
sample TL
If the default currency and the selected currency are the same, set “price * 1” in the field and it will return to the original currency.
interesting situation