Advertisement
borkolivic

MX WC with admin

Mar 12th, 2015
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.65 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('mxwc_option')['mxwc_eur_rate_input'];
  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('mxwc_option')['mxwc_eur_rate_input'];
  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. class MXWC_SettingsPage
  64. {
  65.     /**
  66.      * Holds the values to be used in the fields callbacks
  67.      */
  68.     private $options;
  69.  
  70.     /**
  71.      * Start up
  72.      */
  73.     public function __construct()
  74.     {
  75.         add_action( 'admin_menu', array( $this, 'add_mxwc_plugin_page' ) );
  76.         add_action( 'admin_init', array( $this, 'page_init' ) );
  77.     }
  78.  
  79.     /**
  80.      * Add options page
  81.      */
  82.     public function add_mxwc_plugin_page()
  83.     {
  84.         // This page will be under "Settings"
  85.         add_options_page(
  86.             'MX WC Plugin Page',
  87.             'MX WC fallback rate',
  88.             'manage_options',
  89.             'mxwc_section',
  90.             array( $this, 'create_mxwc_admin_page' )
  91.         );
  92.     }
  93.  
  94.     /**
  95.      * Options page callback
  96.      */
  97.     public function create_mxwc_admin_page()
  98.     {
  99.         // Set class property
  100.         $this->options = get_option( 'mxwc_option' );
  101.         ?>
  102.         <div class="wrap">
  103.             <h2>MX WC Plugin Page</h2>
  104.             <form method="post" action="options.php">
  105.             <?php
  106.                 // This prints out all hidden setting fields
  107.                 settings_fields( 'mxwc_options' );
  108.                 do_settings_sections( 'mxwc_section' );
  109.                 submit_button('Pohrani izmjene');
  110.             ?>
  111.             </form>
  112.         </div>
  113.         <?php
  114.     }
  115.  
  116.     /**
  117.      * Register and add settings
  118.      */
  119.     public function page_init()
  120.     {
  121.         register_setting(
  122.             'mxwc_options', // Option group
  123.             'mxwc_option', // Option name
  124.             array( $this, 'sanitize' ) // Sanitize
  125.         );
  126.  
  127.         add_settings_section(
  128.             'mxwc_section_id', // ID
  129.             'MX WC fallback rate', // Title
  130.             array( $this, 'print_section_info' ), // Callback
  131.             'mxwc_section' // Page
  132.         );
  133.  
  134.         add_settings_field(
  135.             'mxwc_eur_rate_input',
  136.             'Upišite stopu konverzije',
  137.             array( $this, 'eur_rate_callback' ),
  138.             'mxwc_section',
  139.             'mxwc_section_id'
  140.         );
  141.     }
  142.  
  143.     /**
  144.      * Sanitize each setting field as needed
  145.      *
  146.      * @param array $input Contains all settings fields as array keys
  147.      */
  148.     public function sanitize( $input )
  149.     {
  150.         $new_input = array();
  151.  
  152.         if( isset( $input['mxwc_eur_rate_input'] ) )
  153.             $new_input['mxwc_eur_rate_input'] = floatval( $input['mxwc_eur_rate_input'] );
  154.  
  155.         return $new_input;
  156.     }
  157.  
  158.     /**
  159.      * Print the Section text
  160.      */
  161.     public function print_section_info()
  162.     {
  163.         print 'Dolje unesite stopu konverzije HRK u EUR koja će se koristiti u slučaju da nije moguće dohvatiti HNB tečajnicu.';
  164.     }
  165.  
  166.     /**
  167.      * Get the settings option array and print one of its values
  168.      */
  169.     public function eur_rate_callback()
  170.     {
  171.         printf(
  172.             '<input type="text" id="mxwc_eur_rate_input" name="mxwc_option[mxwc_eur_rate_input]" value="%s" />',
  173.             isset( $this->options['mxwc_eur_rate_input'] ) ? esc_attr( $this->options['mxwc_eur_rate_input']) : ''
  174.         );
  175.     }
  176. }
  177.  
  178. if( is_admin() )
  179.     $mxwc_settingspage = new MXWC_SettingsPage();
  180. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement