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('mxwc_option')['mxwc_eur_rate_input'];
- 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('mxwc_option')['mxwc_eur_rate_input'];
- }
- }
- 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 */
- class MXWC_SettingsPage
- {
- /**
- * Holds the values to be used in the fields callbacks
- */
- private $options;
- /**
- * Start up
- */
- public function __construct()
- {
- add_action( 'admin_menu', array( $this, 'add_mxwc_plugin_page' ) );
- add_action( 'admin_init', array( $this, 'page_init' ) );
- }
- /**
- * Add options page
- */
- public function add_mxwc_plugin_page()
- {
- // This page will be under "Settings"
- add_options_page(
- 'MX WC Plugin Page',
- 'MX WC fallback rate',
- 'manage_options',
- 'mxwc_section',
- array( $this, 'create_mxwc_admin_page' )
- );
- }
- /**
- * Options page callback
- */
- public function create_mxwc_admin_page()
- {
- // Set class property
- $this->options = get_option( 'mxwc_option' );
- ?>
- <div class="wrap">
- <h2>MX WC Plugin Page</h2>
- <form method="post" action="options.php">
- <?php
- // This prints out all hidden setting fields
- settings_fields( 'mxwc_options' );
- do_settings_sections( 'mxwc_section' );
- submit_button('Pohrani izmjene');
- ?>
- </form>
- </div>
- <?php
- }
- /**
- * Register and add settings
- */
- public function page_init()
- {
- register_setting(
- 'mxwc_options', // Option group
- 'mxwc_option', // Option name
- array( $this, 'sanitize' ) // Sanitize
- );
- add_settings_section(
- 'mxwc_section_id', // ID
- 'MX WC fallback rate', // Title
- array( $this, 'print_section_info' ), // Callback
- 'mxwc_section' // Page
- );
- add_settings_field(
- 'mxwc_eur_rate_input',
- 'Upišite stopu konverzije',
- array( $this, 'eur_rate_callback' ),
- 'mxwc_section',
- 'mxwc_section_id'
- );
- }
- /**
- * Sanitize each setting field as needed
- *
- * @param array $input Contains all settings fields as array keys
- */
- public function sanitize( $input )
- {
- $new_input = array();
- if( isset( $input['mxwc_eur_rate_input'] ) )
- $new_input['mxwc_eur_rate_input'] = floatval( $input['mxwc_eur_rate_input'] );
- return $new_input;
- }
- /**
- * Print the Section text
- */
- public function print_section_info()
- {
- print 'Dolje unesite stopu konverzije HRK u EUR koja će se koristiti u slučaju da nije moguće dohvatiti HNB tečajnicu.';
- }
- /**
- * Get the settings option array and print one of its values
- */
- public function eur_rate_callback()
- {
- printf(
- '<input type="text" id="mxwc_eur_rate_input" name="mxwc_option[mxwc_eur_rate_input]" value="%s" />',
- isset( $this->options['mxwc_eur_rate_input'] ) ? esc_attr( $this->options['mxwc_eur_rate_input']) : ''
- );
- }
- }
- if( is_admin() )
- $mxwc_settingspage = new MXWC_SettingsPage();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement