Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter('woocommerce_paypal_args', 'convert_hrk_to_eur');
- function get_currency() {
- $eur_rate = get_transient( 'eur_rate' );
- if ( empty( $eur_rate ) ){
- $response = wp_remote_get( 'http://hnbex.eu/api/v1/rates/daily');
- if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
- $eur_rate = '7.5';
- try {
- $result = wp_remote_retrieve_body( $response );
- $currency_list = json_decode( $result, true);
- foreach ($currency_list as $item) {
- if ($item['currency_code'] === 'EUR') {
- $eur_rate = $item['median_rate'];
- }
- }
- set_transient( 'eur_rate', $eur_rate, 6 * HOUR_IN_SECONDS );
- } catch ( Exception $ex ) {
- error_log( 'HNBEX API REQUEST ERROR : ' . $ex->getMessage() );
- $eur_rate = '7.5';
- }
- }
- return $eur_rate;
- }
- 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