Skip to content

Instantly share code, notes, and snippets.

@vinefruit
Last active October 16, 2018 14:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vinefruit/65825aab214e1ae4a146 to your computer and use it in GitHub Desktop.
Save vinefruit/65825aab214e1ae4a146 to your computer and use it in GitHub Desktop.
add unsupported paypal currency to woocommerce and woocommerce subscriptions
<?php
/**
* this code will add an unsupported currency to Woocommerce
* it will convert any amounts including those for a subscription product 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)
* note - any orders placed this way are automatically placed on-hold by Woocommerce due to an amount mismatch and MUST be manually updated to processing or complete, esp with virtual or downloadable orders.
*
* 9 April 2018: updated currency converter as Google 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);
}
if (isset($paypal_args['a1'])) {
$paypal_args['a1'] = round( $paypal_args['a1'] / $convert_rate, 2);
$paypal_args['currency_code'] = USD;
}
if (isset($paypal_args['a2'])) {
$paypal_args['a2'] = round( $paypal_args['a2'] / $convert_rate, 2);
$paypal_args['currency_code'] = USD;
}
if (isset($paypal_args['a3'])) {
$paypal_args['a3'] = round( $paypal_args['a3'] / $convert_rate, 2);
$paypal_args['currency_code'] = USD;
}
}
return $paypal_args;
}
@pacificip
Copy link

You made my day !
Thanks a lot :-)

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