Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: MX Woocommerce HRK currency + PayPal + HNB conversion
- Plugin URI: http://media-x.hr
- Description: Plugin for enabling PayPal gateway with HRK set as store currency + HNB conversion
- Version: 1.0
- Author: Media X
- Author URI: http://media-x.hr
- License: GPLv3
- */
- 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 = get_option('plugin_options')['text_string'];
- 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 = get_option('plugin_options')['text_string'];
- }
- }
- 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;
- }
- /* Plugin admin option */
- // add the admin options page
- add_action('admin_menu', 'plugin_admin_add_page');
- function plugin_admin_add_page() {
- add_options_page('MX WC Plugin Page', 'MX WC fallback rate', 'manage_options', 'plugin', 'plugin_options_page');
- }
- // display the admin options page
- function plugin_options_page() {
- ?>
- <div>
- <div><?php echo '<img src="' . plugins_url( 'images/mxwc.jpg', __FILE__ ) . '" > '; ?></div>
- <h2>MXWC plugin</h2>
- <p>Options relating to the MX WC Plugin.</p>
- <form action="options.php" method="post">
- <?php settings_fields('plugin_options'); ?>
- <?php do_settings_sections('plugin'); ?>
- <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
- </form></div>
- <?php
- }
- // add the admin settings
- add_action('admin_init', 'plugin_admin_init');
- function plugin_admin_init(){
- register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' );
- add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text', 'plugin');
- add_settings_field('plugin_text_string', 'Upišite stopu konverzije', 'plugin_setting_string', 'plugin', 'plugin_main');
- }
- function plugin_setting_string() {
- $options = get_option('plugin_options');
- echo "<input id='plugin_text_string' name='plugin_options[text_string]' size='40' type='text' value='{$options['text_string']}' />";
- }
- // validate
- function plugin_options_validate($input) {
- $newinput['text_string'] = trim($input['text_string']);
- if(!preg_match('/^[0-9\.]{3,10}$/', $newinput['text_string'])) {
- $newinput['text_string'] = '';
- }
- return $newinput;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement