Advertisement
borkolivic

hnb

Mar 11th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.39 KB | None | 0 0
  1. $transient = get_transient( 'hnb_rate' );
  2. if ( empty( $transient ) ){
  3.    
  4. add_filter('woocommerce_paypal_args', 'convert_hrk_to_eur');  
  5. function get_currency() {
  6.     $url = "http://hnbex.eu/api/v1/rates/daily/";
  7.  
  8.     $json =  wp_remote_fopen($url);
  9.     $rates = json_decode($json);
  10.  
  11.     foreach ($rates as $rate) {
  12.         if ($rate->currency_code == "EUR") {
  13.             return $rate->median_rate;
  14.         }
  15.     }
  16.  
  17.     trigger_error("No exchange rate available", E_USER_ERROR);
  18. }  
  19. set_transient('hnb_rate', $data, 60*60*12 );
  20. }
  21. add_filter( 'woocommerce_paypal_supported_currencies', 'add_hrk_paypal_valid_currency' );    
  22.     function add_hrk_paypal_valid_currency( $currencies ) {  
  23.      array_push ( $currencies , 'HRK' );
  24.      return $currencies;  
  25.     }
  26. add_filter('woocommerce_paypal_args', 'convert_hrk_to_eur');
  27.  
  28. function convert_hrk_to_eur($paypal_args){
  29.     if ( $paypal_args['currency_code'] == 'HRK'){  
  30.     $convert_rate = get_currency();
  31.     $paypal_args['currency_code'] = 'EUR';
  32.         $i = 1;
  33.  
  34.         while (isset($paypal_args['amount_' . $i])) {  
  35.             $paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
  36.             ++$i;  
  37.         }  
  38.  
  39.         if ( $paypal_args['discount_amount_cart'] > 0 ) {
  40.             $paypal_args['discount_amount_cart'] = round( $paypal_args['discount_amount_cart'] / $convert_rate, 2);
  41.         }  
  42.         }
  43.  
  44. return $paypal_args;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement