WCML_Custom_Currency_Options

The WooCommerce Multilingual WCML Custom Currency Options class.

Defined (1)

The class is defined in the following location(s).

/inc/template-classes/multi-currency/class-wcml-custom-currency-options.php  
  1. class WCML_Custom_Currency_Options extends WPML_Templates_Factory { 
  2.  
  3. private $woocommerce_wpml; 
  4. private $args; 
  5.  
  6. function __construct( &$args, &$woocommerce_wpml ) { 
  7.  
  8. $functions = array( 
  9. new Twig_SimpleFunction( 'get_currency_symbol', array( $this, 'get_currency_symbol' ) ),  
  10. ); 
  11.  
  12. parent::__construct( $functions ); 
  13. $this->woocommerce_wpml = $woocommerce_wpml; 
  14. $this->args = $args; 
  15.  
  16. add_action( 'wcml_before_multi_currency_ui', array($this, 'render') ); 
  17.  
  18. public function get_model() { 
  19.  
  20. $currencies_not_used = array_diff( array_keys( $this->args['wc_currencies'] ),  
  21. array_keys( $this->args['currencies'] ), array( $this->args['default_currency'] ) ); 
  22. $current_currency = empty($this->args['currency_code']) ? current( $currencies_not_used ) : $this->args['currency_code']; 
  23.  
  24. $exchange_rate_services =& $this->woocommerce_wpml->multi_currency->exchange_rate_services; 
  25. $exchange_rates_automatic = $exchange_rate_services->get_setting('automatic'); 
  26.  
  27. if( $exchange_rates_automatic ) { 
  28. $service_id = $exchange_rate_services->get_setting('service'); 
  29. $services = $exchange_rate_services->get_services(); 
  30. $exchange_rates_service = $services[$service_id]->get_name(); 
  31. } else { 
  32. $exchange_rates_service = ''; 
  33.  
  34. $model = array( 
  35.  
  36. 'args' => $this->args,  
  37. 'form' => array( 
  38. 'select' => __( 'Select currency', 'woocommerce-multilingual' ),  
  39. 'rate' => array( 
  40. 'label' => __( 'Exchange Rate', 'woocommerce-multilingual' ),  
  41. 'only_numeric' => __( 'Only numeric', 'woocommerce-multilingual' ),  
  42. 'min' => '0.01',  
  43. 'step' => '0.01',  
  44. 'set_on' => empty($this->args['currency']['updated'] ) ? '' : 
  45. sprintf( __( 'Set on %s', 'woocommerce-multilingual' ),  
  46. date( 'F j, Y g:i a', strtotime( $this->args['currency']['updated'] ) ) ),  
  47. 'previous' => empty($this->args['currency']['previous_rate'] ) ? '' : 
  48. ' ' . sprintf( __( '(previous value: %s)', 'woocommerce-multilingual' ), $this->args['currency']['previous_rate'] ) 
  49. ),  
  50. 'preview' => array( 
  51. 'label' => __( 'Currency Preview', 'woocommerce-multilingual' ),  
  52. 'value' => $this->get_price_preview( $current_currency ) 
  53. ),  
  54. 'position' => array( 
  55. 'label' => __( 'Currency Position', 'woocommerce-multilingual' ),  
  56. 'left' => __( 'Left', 'woocommerce-multilingual' ),  
  57. 'right' => __( 'Right', 'woocommerce-multilingual' ),  
  58. 'left_space' => __( 'Left with space', 'woocommerce-multilingual' ),  
  59. 'right_space' => __( 'Right with space', 'woocommerce-multilingual' ),  
  60. ),  
  61. 'thousand_sep' => array( 
  62. 'label' => __( 'Thousand Separator', 'woocommerce-multilingual' ) 
  63. ),  
  64. 'decimal_sep' => array( 
  65. 'label' =>__( 'Decimal Separator', 'woocommerce-multilingual' ) 
  66. ),  
  67. 'num_decimals' => array( 
  68. 'label' => __( 'Number of Decimals', 'woocommerce-multilingual' ),  
  69. 'only_numeric' => __( 'Only numeric', 'woocommerce-multilingual' ) 
  70. ),  
  71. 'rounding' => array( 
  72. 'label' => __( 'Rounding to the nearest integer', 'woocommerce-multilingual' ),  
  73. 'disabled' => __( 'Disabled', 'woocommerce-multilingual' ),  
  74. 'up' => __( 'Up', 'woocommerce-multilingual' ),  
  75. 'down' => __( 'Down', 'woocommerce-multilingual' ),  
  76. 'nearest' => __( 'Nearest', 'woocommerce-multilingual' ),  
  77. 'increment' => __( 'Increment for nearest integer', 'woocommerce-multilingual' ),  
  78.  
  79. 'rounding_tooltip' => sprintf( __("Round the converted price to the closest integer. %se.g. 15.78 becomes 16.00", 'woocommerce-multilingual'), '<br />' ),  
  80. 'increment_tooltip' => sprintf( __('The resulting price will be an increment of this value after initial rounding.%se.g.:', 'woocommerce-multilingual'), '<br>' ) . '<br />' . 
  81. __('1454.07 » 1454 when set to 1', 'woocommerce-multilingual') . '<br />' . 
  82. __('1454.07 » 1450 when set to 10', 'woocommerce-multilingual') . '<br />'. 
  83. __('1454.07 » 1500 when set to 100', 'woocommerce-multilingual') . '<br />',  
  84. 'autosubtract_tooltip'=> __('The value to be subtracted from the amount obtained previously.', 'woocommerce-multilingual') . '<br /><br />' . 
  85. __('For 1454.07, when the increment for the nearest integer is 100 and the auto-subtract amount is 1, the resulting amount is 1499.', 'woocommerce-multilingual') 
  86. ),  
  87. 'autosubtract' => array( 
  88. 'label' => __( 'Autosubtract amount', 'woocommerce-multilingual' ),  
  89. 'only_numeric' => __( 'Only numeric', 'woocommerce-multilingual' ) 
  90. ),  
  91.  
  92. 'number_error' => __( 'Please enter a valid number', 'woocommerce-multilingual' ),  
  93. 'cancel' => __( 'Cancel', 'woocommerce-multilingual' ),  
  94. 'save' => __( 'Save', 'woocommerce-multilingual' ) 
  95.  
  96.  
  97. ),  
  98.  
  99. 'automatic_rates' => $exchange_rates_automatic,  
  100. 'automatic_rates_tip' => sprintf( __('Exchange rate updated automatically from %s', 'woocommerce-multilingual' ), $exchange_rates_service ),  
  101. 'current_currency' => $current_currency 
  102.  
  103.  
  104. ); 
  105.  
  106. return $model; 
  107.  
  108. public function render() { 
  109. echo $this->get_view(); 
  110.  
  111. protected function init_template_base_dir() { 
  112. $this->template_paths = array( 
  113. WCML_PLUGIN_PATH . '/templates/multi-currency/',  
  114. ); 
  115.  
  116. public function get_template() { 
  117. return 'custom-currency-options.twig'; 
  118.  
  119. public function get_currency_symbol( $code ) { 
  120. return get_woocommerce_currency_symbol( $code ); 
  121.  
  122. public function get_price_preview( $currency ) { 
  123.  
  124. if( isset( $this->args['currencies'][$currency] ) ) { 
  125.  
  126. $this->current_currency_for_preview =& $currency; 
  127.  
  128. add_filter( 'option_woocommerce_currency_pos', array($this, 'filter_currency_pos') ); 
  129.  
  130. $args = array( 
  131. 'currency' => $currency,  
  132. 'decimal_separator' => $this->args['currencies'][$currency]['decimal_sep'],  
  133. 'thousand_separator' => $this->args['currencies'][$currency]['thousand_sep'],  
  134. 'decimals' => $this->args['currencies'][$currency]['num_decimals'],  
  135. 'price_format' => get_woocommerce_price_format() 
  136. ); 
  137. $price = wc_price( '1234.56', $args ); 
  138.  
  139. remove_filter( 'option_woocommerce_currency_pos', array($this, 'filter_currency_pos') ); 
  140.  
  141. unset($this->current_currency_for_preview); 
  142.  
  143. } else { 
  144. $args = array( 
  145. 'currency' => $currency,  
  146. 'price_format' => get_woocommerce_price_format() 
  147. ); 
  148. $price = wc_price( '1234.56', $args ); 
  149.  
  150. return $price; 
  151.  
  152. public function filter_currency_pos( $value ) { 
  153.  
  154. if( isset( $this->args['currencies'][ $this->current_currency_for_preview ]['position'] ) ) { 
  155. $value = $this->args['currencies'][ $this->current_currency_for_preview ]['position']; 
  156.  
  157. return $value; 
  158.  
  159.  
  160.