Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $transient = get_transient( 'hnb_rate' );
- if ( empty( $transient ) ){
- add_filter('woocommerce_paypal_args', 'convert_hrk_to_eur');
- function get_currency() {
- $url = "http://hnbex.eu/api/v1/rates/daily/";
- $json = wp_remote_fopen($url);
- $rates = json_decode($json);
- foreach ($rates as $rate) {
- if ($rate->currency_code == "EUR") {
- return $rate->median_rate;
- }
- }
- trigger_error("No exchange rate available", E_USER_ERROR);
- }
- set_transient('hnb_rate', $data, 60*60*12 );
- }
- add_filter( 'woocommerce_paypal_supported_currencies', 'add_hrk_paypal_valid_currency' );
- function add_hrk_paypal_valid_currency( $currencies ) {
- array_push ( $currencies , 'HRK' );
- return $currencies;
- }
- add_filter('woocommerce_paypal_args', 'convert_hrk_to_eur');
- function convert_hrk_to_eur($paypal_args){
- if ( $paypal_args['currency_code'] == 'HRK'){
- $convert_rate = get_currency();
- $paypal_args['currency_code'] = 'EUR';
- $i = 1;
- while (isset($paypal_args['amount_' . $i])) {
- $paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
- ++$i;
- }
- if ( $paypal_args['discount_amount_cart'] > 0 ) {
- $paypal_args['discount_amount_cart'] = round( $paypal_args['discount_amount_cart'] / $convert_rate, 2);
- }
- }
- return $paypal_args;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement