Advertisement
borkolivic

alessio

Mar 13th, 2015
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.03 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: MX Woocommerce HRK currency + PayPal + HNB conversion
  4. Plugin URI: http://media-x.hr
  5. Description: Plugin for enabling PayPal gateway with HRK set as store currency + HNB conversion
  6. Version: 1.0
  7. Author: Media X
  8. Author URI: http://media-x.hr
  9. License: GPLv3
  10. */
  11.  
  12. add_filter('woocommerce_paypal_args', 'convert_hrk_to_eur');
  13. function get_currency() {
  14.         $eur_rate = get_transient( 'eur_rate' );
  15.         if ( empty( $eur_rate ) ){
  16.                 $response = wp_remote_get( 'http://hnbex.eu/api/v1/rates/daily');
  17.                 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
  18.                         $eur_rate = get_option('plugin_options')['text_string'];
  19.                 try {
  20.                         $result = wp_remote_retrieve_body( $response );
  21.                         $currency_list = json_decode( $result, true);
  22.                         foreach ($currency_list as $item) {
  23.                                 if ($item['currency_code'] === 'EUR') {
  24.                                         $eur_rate = $item['median_rate'];
  25.                                 }
  26.                         }
  27.                         set_transient( 'eur_rate', $eur_rate, 6 * HOUR_IN_SECONDS );
  28.                 } catch ( Exception $ex ) {
  29.                         error_log( 'HNBEX API REQUEST ERROR : ' . $ex->getMessage() );
  30.                         $eur_rate = get_option('plugin_options')['text_string'];
  31.                 }
  32.         }
  33.         return $eur_rate;
  34. }
  35.  
  36. add_filter( 'woocommerce_paypal_supported_currencies', 'add_hrk_paypal_valid_currency' );    
  37.     function add_hrk_paypal_valid_currency( $currencies ) {  
  38.      array_push ( $currencies , 'HRK' );
  39.      return $currencies;  
  40.     }
  41. add_filter('woocommerce_paypal_args', 'convert_hrk_to_eur');
  42.  
  43. function convert_hrk_to_eur($paypal_args){
  44.         if ( $paypal_args['currency_code'] == 'HRK'){  
  45.         $convert_rate = get_currency();
  46.         $paypal_args['currency_code'] = 'EUR';
  47.                 $i = 1;
  48.  
  49.         while (isset($paypal_args['amount_' . $i])) {  
  50.             $paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
  51.             ++$i;  
  52.         }  
  53.  
  54.                 if ( $paypal_args['discount_amount_cart'] > 0 ) {
  55.                         $paypal_args['discount_amount_cart'] = round( $paypal_args['discount_amount_cart'] / $convert_rate, 2);
  56.                 }  
  57.                 }
  58.  
  59. return $paypal_args;  
  60. }
  61. /* Plugin admin option */
  62.  
  63. // add the admin options page
  64. add_action('admin_menu', 'plugin_admin_add_page');
  65. function plugin_admin_add_page() {
  66. add_options_page('MX WC Plugin Page', 'MX WC fallback rate', 'manage_options', 'plugin', 'plugin_options_page');
  67. }
  68. // display the admin options page
  69. function plugin_options_page() {
  70. ?>
  71. <div>
  72. <div><?php echo '<img src="' . plugins_url( 'images/mxwc.jpg', __FILE__ ) . '" > '; ?></div>
  73. <h2>MXWC plugin</h2>
  74. <p>Options relating to the MX WC Plugin.</p>
  75. <form action="options.php" method="post">
  76. <?php settings_fields('plugin_options'); ?>
  77. <?php do_settings_sections('plugin'); ?>
  78.  
  79. <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
  80. </form></div>
  81. <?php
  82. }
  83. // add the admin settings
  84. add_action('admin_init', 'plugin_admin_init');
  85. function plugin_admin_init(){
  86. register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' );
  87. add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text', 'plugin');
  88. add_settings_field('plugin_text_string', 'Upišite stopu konverzije', 'plugin_setting_string', 'plugin', 'plugin_main');
  89. }
  90. function plugin_setting_string() {
  91. $options = get_option('plugin_options');
  92. echo "<input id='plugin_text_string' name='plugin_options[text_string]' size='40' type='text' value='{$options['text_string']}' />";
  93. }
  94. // validate
  95. function plugin_options_validate($input) {
  96. $newinput['text_string'] = trim($input['text_string']);
  97. if(!preg_match('/^[0-9\.]{3,10}$/', $newinput['text_string'])) {
  98. $newinput['text_string'] = '';
  99. }
  100. return $newinput;
  101. }
  102. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement