Advertisement
borkolivic

HRK + PP + HNB

Mar 11th, 2015
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.65 KB | None | 0 0
  1. add_filter('woocommerce_paypal_args', 'convert_hrk_to_eur');
  2. function get_currency() {
  3.     $eur_rate = get_transient( 'eur_rate' );
  4.     if ( empty( $eur_rate ) ){
  5.         $response = wp_remote_get( 'http://hnbex.eu/api/v1/rates/daily');
  6.         if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
  7.             $eur_rate = '7.5';
  8.         try {
  9.             $result = wp_remote_retrieve_body( $response );
  10.             $currency_list = json_decode( $result, true);
  11.             foreach ($currency_list as $item) {
  12.                 if ($item['currency_code'] === 'EUR') {
  13.                     $eur_rate = $item['median_rate'];
  14.                 }
  15.             }
  16.             set_transient( 'eur_rate', $eur_rate, 6 * HOUR_IN_SECONDS );
  17.         } catch ( Exception $ex ) {
  18.             error_log( 'HNBEX API REQUEST ERROR : ' . $ex->getMessage() );
  19.             $eur_rate = '7.5';
  20.         }
  21.     }
  22.     return $eur_rate;
  23. }
  24.  
  25. add_filter( 'woocommerce_paypal_supported_currencies', 'add_hrk_paypal_valid_currency' );    
  26.     function add_hrk_paypal_valid_currency( $currencies ) {  
  27.      array_push ( $currencies , 'HRK' );
  28.      return $currencies;  
  29.     }
  30. add_filter('woocommerce_paypal_args', 'convert_hrk_to_eur');
  31.  
  32. function convert_hrk_to_eur($paypal_args){
  33.     if ( $paypal_args['currency_code'] == 'HRK'){  
  34.     $convert_rate = get_currency();
  35.     $paypal_args['currency_code'] = 'EUR';
  36.         $i = 1;
  37.  
  38.         while (isset($paypal_args['amount_' . $i])) {  
  39.             $paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
  40.             ++$i;  
  41.         }  
  42.  
  43.         if ( $paypal_args['discount_amount_cart'] > 0 ) {
  44.             $paypal_args['discount_amount_cart'] = round( $paypal_args['discount_amount_cart'] / $convert_rate, 2);
  45.         }  
  46.         }
  47.  
  48. return $paypal_args;  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement