How to add a new currency to WPDeals (The real way)

I’m writing this post because I’ve been working with WPDeals for about a day, and while I like the plugin I noticed right off the bat that it does not support all currencies. For example AED (Emirati Dirhams) was not supported.

Now before I go any further, I want to mention that PayPal do not support every currency either. For example Paypal Standard does not support AED, but I wanted to get around this limitation, and give the end user a seamless experience.

I also want to tell you, I am not a PHP developer. The first time I’ve written any PHP was today. I don’t expect you to be one either, or else you wouldn’t need to read this.

So here is the deal. If you want to implement a new currency into WP deals AND link up payments via your Paypal standard account in an unsupported currency you’ve come to the right place. I’ll show you step by step and leave out no details, so even the biggest noob can just follow these instructions step by step.

So first the plan,

The plan 

  • Add a new currency (cleanly / no messy hacks) to WPDeals
  • Make it work with Paypal standard

How are we going to make it work with Paypal standard?

Our whole site will display our new currency, in this example AED. Finally when a user is ready to purchase, we will do a currency conversion to dollars, and redirect the dollar amount to PayPal standard. loving this yet? Our customers will then see a dollar total in Paypal, and pay that amount, and get redirected back to our site seamlessly.

The Steps

Go ahead and get your coffee. Here are the exact steps you need to implement.

Step 1 : Adding a new currency option to the admin currency list

1.1 Locate and open \wp-content\plugins\wp-deals\wpdeals-admin\admin-settings.php

Here we will make a small change:

</pre>
array(
 'name' => __( 'Currency', 'wpdeals' ),
 'desc' => __("This controls what currency prices are listed at in the deals and which currency gateways will take payments in.", 'wpdeals' ),
 'tip' => '',
 'id' => 'wpdeals_currency',
 'css' => 'min-width:300px;',
 'std' => 'GBP',
 'type' => 'select',
 'class' => 'chosen_select',
 'options' => array_unique(apply_filters('wpdeals_currencies', array(
 'AED' => __( 'Emirati Dirhams (AED)', 'wpdeals' ),
<pre>

Pay special attention to 'AED' => __( 'Emirati Dirhams (AED)', 'wpdeals' ),
This is what we've added to the list.... I've added it to the top, because this is where I want it, but you can decide where you want it in the list, if that is important to you. All we've done here is added the item to the list, but as you will soon see, even if the admin selects this new option, the currency symbol will end up as pounds (or default). We'll fix this in the rest of the steps. 

Step 2

Once you’ve saved the change locally, upload it to your WP install using FTP and overwrite the file. Reload the WP Deals settings page when you are logged in as adminstrator , and you’ll see the option now in the list… Like this:

AdminSettingsMake sure you select it, then save the settings. We’ll need this setting saved to test the rest of the stuff.

Step 3

Locate: \wp-content\plugins\wp-deals\wpdeals.php

Here we’ll edit get_wpdeals_currency_symbol() function


function get_wpdeals_currency_symbol() {
 $currency = get_option('wpdeals_currency');
 $currency_symbol = '';
 switch ($currency) :
 case 'AED' : $currency_symbol = 'AED'; break;
 case 'AUD' :
 case 'BRL' :
 case 'CAD' :

Notice case 'AED' : $currency_symbol = 'AED'; break; that is what we want to insert here.... This means that every time a price is requested by the plugin, this function gets called to display the appropriate currency symbol. So that is why we replace it here, and nowhere else, because this is the best location to add this option.

Step 4

Obviously upload the file to your WP install, and refresh the site, you’ll now see the prices are in your new currency, awesome!

Now what about Paypal?

Step 5 Paypal

first thing we want to do is make sure that the paypal option is enabled with this new currency.

Locate: \wp-content\plugins\wp-deals\wpdeals-classes\gateways\gateway-paypal.php

</code></pre>
/**
 * Check if this gateway is enabled and available in the user's country
 */
 function is_valid_for_use() {
 if (!in_array(get_option('wpdeals_currency'), array('AED', 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP'))) return false;

return true;
 }
<pre><code>

Notice array('AED' this is what we are adding. We're basically adding the currency code to this list, by doing this we're telling WP Deals that the AED currency is valid for use with Paypal. 

Now after doing this, you can setup Paypal in admin, and as a user select and pay using Paypal. But this still doesn’t solve the currency issue – because Paypal doesn’t support your currency right?

Step 6 : Currency conversion

Important: I wanted to use Google for currency conversion, but since they’re shutting that service down in Nov 2013, I found another solution. Something that hopefully will last.

Open up : \wp-content\plugins\wp-deals\wpdeals-classes\gateways\gateway-paypal.php

At the very top, paste in the class only 


<?php
/**
 * PayPal Standard Payment Gateway
 *
 * Provides a PayPal Standard Payment Gateway.
 *
 * @class wpdeals_paypal
 * @package WPDeals
 * @category Payment Gateways
 * @author Tokokoo
 */

class ForeignExchange
{
 private $fxRate;

public function __construct($currencyBase, $currencyForeign)
 {
 $url = 'http://download.finance.yahoo.com/d/quotes.csv?s='
 .$currencyBase .$currencyForeign .'=X&f=l1';

$c = curl_init($url);
 curl_setopt($c, CURLOPT_HEADER, 0);
 curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
 $this->fxRate = doubleval(curl_exec($c));
 curl_close($c);
 }

public function toBase($amount)
 {
 if($this->fxRate == 0)
 return 0;

return $amount / $this->fxRate;
 }

public function toForeign($amount)
 {
 if($this->fxRate == 0)
 return 0;

return $amount * $this->fxRate;
 }
};

This is the class that will do the work getting us a currency conversion rate for the day.

Step 7

It might be surprising but now we won’t be changing the total we send to Paypal, but rather the price of each item in the shopping cart.

So here once again in : \wp-content\plugins\wp-deals\wpdeals-classes\gateways\gateway-paypal.php

</pre>
$paypal_args = array(
 'cmd' => '_cart',
 'business' => $this->email,
 'no_note' => 1,
 'currency_code' => 'USD',
 'charset' => 'UTF-8',
 'rm' => 2,
 'upload' => 1,
 'return' => $this->get_return_url( $order ),
 'cancel_return' => $order->get_cancel_order_url(),
 'no_shipping' => 1,
<pre>

Notice ‘currency_code’ => ‘USD’. It is important you change this, so that Paypal knows what to expect. I’ve just gone with Dollars, since it seems like the least surprising currency to my target audience.

Next we need to implement the actual currency conversion per item in the cart.

</pre>
// Cart Contents
 $item_loop = 0;
 if (sizeof($order->items)>0) : foreach ($order->items as $item) :
 if ($item['qty']) :

$item_loop++;

$item_name = $item['name'];

$item_meta = &new order_item_meta( $item['item_meta'] );
 if ($meta = $item_meta->display( true, true )) :
 $item_name .= ' ('.$meta.')';
 endif;
 $fx = new ForeignExchange('AED', 'USD');

$paypal_args['item_name_'.$item_loop] = $item_name;
 $paypal_args['quantity_'.$item_loop] = $item['qty'];
 $paypal_args['amount_'.$item_loop] = number_format($fx->toForeign(($item['cost']), 2, '.', '')) ;

endif;
 endforeach; endif;
<pre>

Notice: $fx = new ForeignExchange(‘AED’, ‘USD’); This is a new instance of the class we created in step 6.

ALSO NOTICE

$paypal_args[‘amount_’.$item_loop] = number_format($fx->toForeign(($item[‘cost’]), 2, ‘.’, ”)) ;

After that the whole thing should work 🙂

Enjoy, leave a comment if this helped you!