Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vinefruit
Last active February 6, 2019 07:21
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vinefruit/3eb76c85707dcd254841 to your computer and use it in GitHub Desktop.
Save vinefruit/3eb76c85707dcd254841 to your computer and use it in GitHub Desktop.
add unsupported paypal currency to woocommerce
<?php
/**
* this code will add an unsupported currency to Woocommerce (tested up to v3.1.2 on WP 4.8.2)
* it will convert any amounts including a cart discount, tax or shipping into a supported currency (in this case USD)
* paypal HTML variables can be found at https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/#id08A6HI00JQU
* payment processing can then be done via Paypal
* this example uses the UAE Dirham (or AED). Change the currency symbol and abbreviation to suit your environment.
* note - any orders placed this way are automatically placed on-hold by Woocommerce due to an amount & currency mismatch and MUST be manually updated to processing or complete, esp with virtual or downloadable orders.
* stock or inventory management does NOT work with this workaround - there is an additional step needed please see https://vinefruit.net/woocommerce-currency-tweak/
*
* 9 April 2018: updated currency converter URL as Google one stopped working
*/
/*Step 1 Code to use AED currency to display Dirhams in WooCommerce:*/
add_filter( 'woocommerce_currencies', 'add_aed_currency' );
function add_aed_currency( $currencies ) {
$currencies['AED'] = __( 'UAE Dirham', 'woocommerce' );
return $currencies;
}
/*Step 2 Code to add AED currency symbol in WooCommerce:*/
add_filter('woocommerce_currency_symbol', 'add_aed_currency_symbol', 10, 2);
function add_aed_currency_symbol( $currency_symbol, $currency ) {
switch( $currency ) {
case 'AED': $currency_symbol = 'AED'; break;
}
return $currency_symbol;
}
add_filter( 'woocommerce_paypal_supported_currencies', 'add_aed_paypal_valid_currency' );
function add_aed_paypal_valid_currency( $currencies ) {
array_push ( $currencies , 'AED' );
return $currencies;
}
/*Step 3 – Code to change 'AED' currency to ‘USD’ before checking out with Paypal through WooCommerce:*/
add_filter('woocommerce_paypal_args', 'convert_aed_to_usd', 11 );
function get_currency($from, $to){
$from = "USD";
$to = "AED";
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://free.currencyconverterapi.com/api/v5/convert?q={$from}_{$to}&compact=ultra");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$data = explode(':', $output);
$data = explode(" ", $data[1]);
$amnt = round($data[0], 2);
return $amnt;
}
function convert_aed_to_usd($paypal_args){
if ( $paypal_args['currency_code'] == 'AED'){
$convert_rate = get_currency(); //Set converting rate
$paypal_args['currency_code'] = 'USD'; //change AED to USD
$i = 1;
while (isset($paypal_args['amount_' . $i])) {
$paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
++$i;
}
if ( $paypal_args['shipping_1'] > 0 ) {
$paypal_args['shipping_1'] = round( $paypal_args['shipping_1'] / $convert_rate, 2);
}
if ( $paypal_args['discount_amount_cart'] > 0 ) {
$paypal_args['discount_amount_cart'] = round( $paypal_args['discount_amount_cart'] / $convert_rate, 2);
}
if ( $paypal_args['tax_cart'] > 0 ) {
$paypal_args['tax_cart'] = round( $paypal_args['tax_cart'] / $convert_rate, 2);
}
}
return $paypal_args;
}
@Rappadappa77
Copy link

Hello,

Thank you for all the work you have done, is this the latest code for wordpress and woocommerce, will like to give it a try and see?

Howard

@Rappadappa77
Copy link

Thank you for all your work, the only thing that ever worked for me 💯 👍

@alghazu
Copy link

alghazu commented Dec 12, 2016

Thanks a lot, that was very useful, works like a charm.

@501digitalmedia
Copy link

Thanks! Your code works perfectly. Could you please tell me how to use the step 3 with more than one currency? I'm using a currency switcher and I tried it with other currencies and it gave me an error. Can't use "get_currency" again.

@therampagerado
Copy link

How I must edit the code so I have fixed exchange ratio not Google's?

@vinefruit
Copy link
Author

vinefruit commented Mar 24, 2017

Hi all, sorry I am not notified of comments, please contact me via my website https://vinefruit.net should you get stuck.

@501digitalmedia - I'm not sure how you would use this for more than one currency sorry. Maybe try to use the paypal express plugin instead of paypal std - https://woocommerce.com/products/woocommerce-gateway-paypal-express-checkout/
The PayPal Express extension will allow you to charge in more currencies, but you'll need to set your shop base currency to match. You can learn how to do that here: http://docs.woocommerce.com/document/shop-currency/


@therampagerado - yes there is a way, use this instead of Step 3. In this case 1 USD = 3.68 AED so adjust your currency rate accordingly below

/Step 3 – Code to change ‘AED’ currency to ‘USD’ before checking out with Paypal through WooCommerce:/

add_filter('woocommerce_paypal_args', 'convert_aed_to_usd');
function convert_aed_to_usd($paypal_args){
if ( $paypal_args['currency_code'] == 'AED'){
$convert_rate = 3.68; //set the converting rate
$paypal_args['currency_code'] = 'USD'; //change AED to USD
$i = 1;

	while (isset($paypal_args['amount_' . $i])) {  
		$paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
		++$i;  
    }  
	if ( $paypal_args['shipping_1'] > 0 ) {
		$paypal_args['shipping_1'] = round( $paypal_args['shipping_1'] / $convert_rate, 2);
	}
	
	if ( $paypal_args['discount_amount_cart'] > 0 ) {
		$paypal_args['discount_amount_cart'] = round( $paypal_args['discount_amount_cart'] / $convert_rate, 2);
	}
	if ( $paypal_args['tax_cart'] > 0 ) {
		$paypal_args['tax_cart'] = round( $paypal_args['tax_cart'] / $convert_rate, 2);
	}
}

return $paypal_args;
}

@sitesme
Copy link

sitesme commented Nov 22, 2017

Thanks for this @vinefruit
I am looking for this but for multiple currencies, exactly like @501digitalmedia reported.

Unfortunately I am not a developer, otherwise, it wouldn't be too hard to add more currencies to the script, right?

If anyone has the solution, please share. I will do the same if I find the solution.

Cheers!

Copy link

ghost commented Jan 15, 2018

Greatly appreciated @vinefruit. You saved me a ton of time. Thanks very much!

@tileng1
Copy link

tileng1 commented Jul 30, 2018

Hey, do you by any chance have an updated version of this? Its seems the amount is not getting passed. Thanks

@fariasmaiquita
Copy link

is there a specific paypal payment plugin that needs to be used in order for this to work? those of you who were to get it to work, can you share what paypal plugin are you using? I'm having a hard time getting this to work, I'm using WooCommerce PayPal Checkout Gateway by WooCommerce.. Any help is greatly appreciated..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment