WCML_Currency_Switcher_Template

The WooCommerce Multilingual WCML Currency Switcher Template class.

Defined (1)

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

/inc/template-classes/currency-switcher/class-wcml-currency-switcher-template.php  
  1. class WCML_Currency_Switcher_Template extends WPML_Templates_Factory { 
  2.  
  3. const FILENAME = 'template.twig'; 
  4.  
  5. /** @var array $template */ 
  6. private $template; 
  7.  
  8. /** @var string $prefix */ 
  9. private $prefix = 'wcml-cs-'; 
  10.  
  11. /** 
  12. * @var woocommerce_wpml 
  13. */ 
  14. private $woocommerce_wpml; 
  15.  
  16.  
  17. function __construct( &$woocommerce_wpml, $template_data ) { 
  18.  
  19. $this->woocommerce_wpml =& $woocommerce_wpml; 
  20.  
  21. $this->template = $this->format_data( $template_data ); 
  22.  
  23. if ( array_key_exists( 'template_string', $this->template ) ) { 
  24. $this->template_string = $this->template['template_string']; 
  25.  
  26. $functions = array( 
  27. new Twig_SimpleFunction( 'get_formatted_price', array( $this, 'get_formatted_price' ) ) 
  28. ); 
  29.  
  30. parent::__construct( $functions ); 
  31.  
  32.  
  33. /** 
  34. * @param array $model 
  35. */ 
  36. public function set_model( $model ) { 
  37. $this->model = is_array( $model ) ? $model : array( $model ); 
  38.  
  39. /** 
  40. * @return array 
  41. */ 
  42. public function get_model() { 
  43. return $this->model; 
  44.  
  45. public function render() { 
  46. echo $this->get_view(); 
  47.  
  48. public function get_formatted_price( $currency, $format ) { 
  49.  
  50. $wc_currencies = get_woocommerce_currencies(); 
  51. if( preg_match( '#%subtotal%#', $format ) ) { // include cart total 
  52. $cart_object =& $this->woocommerce_wpml->cart; 
  53. $wcml_settings = $this->woocommerce_wpml->get_settings(); 
  54. $multi_currency =& $this->woocommerce_wpml->multi_currency; 
  55. $client_currency = $multi_currency->get_client_currency(); 
  56.  
  57. if( preg_match( '#%subtotal%#', $format ) ) { // include cart total 
  58. if( !is_admin() ) { 
  59.  
  60. $multi_currency->set_client_currency( $currency ); 
  61. $cart_object->woocommerce_calculate_totals( WC()->cart, $currency ); 
  62. $cart_subtotal = WC()->cart->get_cart_subtotal(); 
  63.  
  64. }else{ 
  65. switch( $wcml_settings['currency_options'][$currency]['position'] ) { 
  66. case 'left' : 
  67. $price_format = '%1$s%2$s'; 
  68. break; 
  69. case 'right' : 
  70. $price_format = '%2$s%1$s'; 
  71. break; 
  72. case 'left_space' : 
  73. $price_format = '%1$s %2$s'; 
  74. break; 
  75. case 'right_space' : 
  76. $price_format = '%2$s %1$s'; 
  77. break; 
  78. $cart_subtotal = wc_price('1234.56',  
  79. array( 
  80. 'currency' => $currency,  
  81. 'decimal_separator' => $wcml_settings['currency_options'][$currency]['decimal_sep'],  
  82. 'thousand_separator' => $wcml_settings['currency_options'][$currency]['thousand_sep'],  
  83. 'decimals' => $wcml_settings['currency_options'][$currency]['num_decimals'],  
  84. 'price_format' => $price_format 
  85. ); 
  86. }else{ 
  87. $cart_subtotal = false; 
  88.  
  89. $currency_format = preg_replace( array('#%name%#', '#%symbol%#', '#%code%#', '#%subtotal%#'),  
  90. array( 
  91. $wc_currencies[$currency],  
  92. get_woocommerce_currency_symbol( $currency ),  
  93. $currency,  
  94. $cart_subtotal 
  95.  
  96. ), $format ); 
  97.  
  98. if( preg_match( '#%subtotal%#', $format ) && !is_admin() ) { // include cart total 
  99. $multi_currency->set_client_currency( $client_currency ); 
  100.  
  101. return $currency_format; 
  102.  
  103. /** 
  104. * Make sure some elements are of array type 
  105. * @param array $template_data 
  106. * @return array 
  107. */ 
  108. private function format_data( $template_data ) { 
  109. foreach ( array( 'path', 'js', 'css' ) as $k ) { 
  110. $template_data[ $k ] = isset( $template_data[ $k ] ) ? $template_data[ $k ] : array(); 
  111. $template_data[ $k ] = is_array( $template_data[ $k ] ) ? $template_data[ $k ] : array( $template_data[ $k ] ); 
  112.  
  113. return $template_data; 
  114.  
  115.  
  116. /** 
  117. * @param bool $with_version 
  118. * @return array 
  119. */ 
  120. public function get_styles( $with_version = false ) { 
  121. return $with_version 
  122. ? array_map( array( $this, 'add_resource_version' ), $this->template['css'] ) 
  123. : $this->template['css']; 
  124.  
  125. /** 
  126. * @return bool 
  127. */ 
  128. public function has_styles() { 
  129. return ! empty( $this->template['css'] ); 
  130.  
  131. /** 
  132. * @param bool $with_version 
  133. * @return array 
  134. */ 
  135. public function get_scripts( $with_version = false ) { 
  136. return $with_version 
  137. ? array_map( array( $this, 'add_resource_version' ), $this->template['js'] ) 
  138. : $this->template['js']; 
  139.  
  140. /** 
  141. * @param string $url 
  142. * @return string 
  143. */ 
  144. private function add_resource_version( $url ) { 
  145. return $url . '?ver=' . WCML_VERSION; 
  146.  
  147. /** 
  148. * @param int $index 
  149. * @return string 
  150. */ 
  151. public function get_resource_handler( $index ) { 
  152. $slug = isset( $this->template['slug'] ) ? $this->template['slug'] : ''; 
  153. $prefix = $this->is_core() ? '' : $this->prefix; 
  154. return $prefix . $slug . '-' . $index; 
  155.  
  156. public function get_inline_style_handler() { 
  157. $count = count( $this->template['css'] ); 
  158. return $count > 0 ? $this->get_resource_handler( $count - 1 ) : null; 
  159.  
  160. protected function init_template_base_dir() { 
  161. $this->template_paths = $this->template['path']; 
  162.  
  163. /** 
  164. * @return string Template filename 
  165. */ 
  166. public function get_template() { 
  167. $template = self::FILENAME; 
  168.  
  169. if ( isset( $this->template_string ) ) { 
  170. $template = $this->template_string; 
  171. } elseif ( array_key_exists( 'filename', $this->template ) ) { 
  172. $template = $this->template['filename']; 
  173.  
  174. return $template; 
  175.  
  176. /** 
  177. * @return array 
  178. */ 
  179. public function get_template_data() { 
  180. return $this->template; 
  181.  
  182. /** 
  183. * return bool 
  184. */ 
  185. public function is_core() { 
  186. return isset( $this->template['is_core'] ) ? (bool) $this->template['is_core'] : false;