You are here:  » Currency conversion

Support Forum



Currency conversion

Submitted by daem0n on Sun, 2009-11-15 08:55 in

Hi David,

First-off, thanks a lot for your help the other day - I just implemented the new files tonight and they work great (as usual).

I wanted to ask you - what do you think is the best method for implementing a method of price conversion? It was discussed a bit in this thread but the user never responded:
http://www.pricetapestry.com/node/2838

I'm really only concerned about two currencies - US and Euro. I'd like to convert the US prices to Euro prices. Some of the feeds have a field that can be registered for the currency but others don't (I could always create a field perhaps on feed registration?).

My main concern is converting the currencies for the user and comparing prices (perhaps showing the user that this is an estimated price?). I could get the currency exchange on an hourly or daily basis from a site or something?

What do you think?

Thanks a lot, -Joe

Submitted by support on Sun, 2009-11-15 09:00

Hi Joe,

Just to confirm, you are running a site predominantly EUR (so having EUR as your display currency), but you would like to incorporate some feeds that have USD and convert them during import to EUR for comparison alongside the EUR feeds?

A similar requirement is discussed here which may help...
http://www.pricetapestry.com/node/1258

Cheers,
David.

Submitted by daem0n on Sun, 2009-11-15 09:15

Hi David,

You are correct. Thanks for that link, it helps me understand that method a bit better. Do you think that's the best method of implementation?...and if so, how would I go about creating a new table for the separate currency, etc?

And one last question (if you do think that the method above is the best implementation of this) - instead of "hard-coding" the conversion rate into the two files, can I have the two files call an external file with the rate posted in it and apply the rate?...this way I can replace the rate on a frequent basis.

Thanks!

Submitted by support on Sun, 2009-11-15 09:32

Hi Joe,

It could actually be more straight forward; rather than using separate tables; just apply the conversion at import time and then display "Approx." (or as required) throughout the site.

One way to do this, using an external file as you mentioned, would be to hold an array of merchants that are in USD; together with the EUR:USD exchange rate - then that would be the only file that you have to maintain; for example:

includes/usd.php

<?php
  $usdMerchants
[] = "Merchant 1";
  
$usdMerchants[] = "Merchant 2";
  
$usdMerchants[] = "Merchant 3";
  
$usdRate 0.670421;
?>

To make this file be included on all pages; edit includes/common.php and ADD the following line at the end of the script (just inside the closing PHP tag):

  require($common_path."usd.php");

With that in place; to apply the conversion during import, look in includes/admin.php for the following code around line 168:

$record[$admin_importFeed["field_price"]] = tapestry_decimalise($record[$admin_importFeed["field_price"]]);

...and REPLACE that with:

$record[$admin_importFeed["field_price"]] = tapestry_decimalise($record[$admin_importFeed["field_price"]]);
global $usdMerchants;
global $usdRate;
if (in_array($admin_importFeed["merchant"],$usdMerchants))
{
  $record[$admin_importFeed["field_price"]] =
    ($record[$admin_importFeed["field_price"]] * $usdRate);
}

Finally; to show where prices are estimated within the price comparison table; look for the following code around line 13:

<td><strong><?php print $config_currencyHTML.$product["price"]; ?></strong></td>

...and REPLACE with:

<td>
<strong><?php print $config_currencyHTML.$product["price"]; ?></strong>
<?php
  if (in_array($product["merchant"],$usdMerchants))
    print "<small><br />(estimated)</small>";
?>
</td>

Hope this helps!

Cheers,
David.

Submitted by daem0n on Sun, 2009-11-15 10:25

Wow amazing, that helps tremendously! I still insist you're some kind of robot haha. I'll work on implementing this and let you know how it goes. Thanks again!

Submitted by bat on Mon, 2011-09-05 14:51

Hi David, I'm trying this out on {link saved} (it's the latest PT version) and the merchant in question is MMA Warehouse. It's in dollars and I want to change it to GBP.
I've followed the above code you did for user daem0n, but for includes/admin.php i couldn't find the corresponding code, so I did this:

/* decimalise price */
$importRecord["price"] = tapestry_decimalise($importRecord["price"]);
global $usdMerchants;
global $usdRate;
if (in_array($admin_importFeed["merchant"],$usdMerchants))
{
$record[$admin_importFeed["field_price"]] =
($record[$admin_importFeed["field_price"]] * $usdRate);
}

However, when I've checked the site after uploading the changed files and re-importing MMA Warehouse, the currency hasn't changed. All it says now is (estimated) under the price but the price is exactly the same as the dollar price.

Is the code I included the culprit or could something else have gone wrong?

Submitted by support on Mon, 2011-09-05 15:23

Hi Bat,

Since support for either multiple feeds for the same merchant or multiple merchants in the same feed was added (11/09A) "merchant" is an importRecord field so the above code would now be:

/* decimalise price */
$importRecord["price"] = tapestry_decimalise($importRecord["price"]);
global $usdMerchants;
global $usdRate;
if (in_array($importRecord["merchant"],$usdMerchants))
{
$record[$admin_importFeed["field_price"]] =
($record[$admin_importFeed["field_price"]] * $usdRate);
}

However, at the point where the /* decimalise price */ section of code is executed, $importRecord["merchant"] has not yet been populated so what you will need to do is move that block down to below where it has been populated. Just a few lines below the section of code that you have already identified look for the comment /* construct merchant value */
. If you take the above code and move it to below that section it should then work fine!

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Mon, 2011-09-05 15:44

Hi David
Thanks for the quick reply. I've done as you suggested but it's no different.

Submitted by support on Mon, 2011-09-05 16:08

Hi Bat,

Could you email me your modified includes/admin.php plus the other files relevant to the mod and I'll check it out...

Cheers,
David.
--
PriceTapestry.com

Submitted by bem on Mon, 2011-12-26 23:07

Hi David ,

I was wondering if there was a simple method of converting prices from 2 of my feeds to pounds. The majority of my feeds are in pounds but I have 2 which are in dollars, which would really help with sales. Currently though they are showing as more expensive than products over here when in fact they are cheaper. Can you help me please?

Thanks
Ben

Submitted by support on Tue, 2011-12-27 09:53

Hi Ben,

Here's code for a generic currency conversion filter which you could add to the Price field of the USD feeds, and then re-import. Copy and paste the following code into the end of your includes/filter.php file; just before the closing PHP tag:

  /*************************************************/
  /* currencyConvert */
  /*************************************************/
  $filter_names["currencyConvert"] = "Currency Convert";
  function filter_currencyConvertConfigure($filter_data)
  {
    print "Rate:<br />";
    print "<input type='text' size='40' name='rate' value='".widget_safe($filter_data["rate"])."' />";
    widget_errorGet("rate");
  }
  function filter_currencyConvertValidate($filter_data)
  {
    if (!is_numeric($filter_data["rate"]))
    {
      widget_errorSet("rate","required numeric field");
    }
  }
  function filter_currencyConvertExec($filter_data,$text)
  {
    $text = $text * $filter_data["rate"];
    return sprintf("%.2f",$text);
  }

For USD > GPB, the rate is currently approx. 0.64 - don't forget to keep this updated in the filter configuration on a regular basis as you update / import your feeds for the most accurate conversion...

Cheers,
David.
--
PriceTapestry.com

Submitted by bem on Sat, 2011-12-31 11:47

That's perfect, such an easy method for converting the currency. Thank you.

Ben