WCML_Currency_Switcher_UI

The WooCommerce Multilingual WCML Currency Switcher UI class.

Defined (1)

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

/inc/template-classes/multi-currency/class-wcml-currency-switcher-ui.php  
  1. class WCML_Currency_Switcher_UI extends WPML_Templates_Factory { 
  2.  
  3. /** 
  4. * @var woocommerce_wpml 
  5. */ 
  6. private $woocommerce_wpml; 
  7. /** 
  8. * @var array 
  9. */ 
  10. private $args; 
  11. /** 
  12. * @var array 
  13. */ 
  14. private $currencies; 
  15.  
  16. function __construct( &$args, &$woocommerce_wpml, $currencies ) { 
  17.  
  18. $this->woocommerce_wpml =& $woocommerce_wpml; 
  19. $this->args = $args; 
  20. $this->currencies = $currencies; 
  21.  
  22. $functions = array( 
  23. new Twig_SimpleFunction( 'get_formatted_price', array( $this, 'get_formatted_price' ) ) 
  24. ); 
  25.  
  26. parent::__construct( $functions ); 
  27.  
  28.  
  29. public function get_model() { 
  30.  
  31. $model = array( 
  32.  
  33. 'style' => isset( $this->args['switcher_style'] ) ? $this->args['switcher_style'] : 'dropdown',  
  34. 'orientation' => isset( $this->args['orientation'] ) && $this->args['orientation'] === 'horizontal' ? 
  35. 'curr_list_horizontal' : 'curr_list_vertical',  
  36. 'format' => $this->args['format'],  
  37.  
  38. 'currencies' => $this->currencies,  
  39.  
  40. 'selected_currency' => $this->woocommerce_wpml->multi_currency->get_client_currency() 
  41.  
  42. ); 
  43.  
  44.  
  45. return $model; 
  46.  
  47. public function get_formatted_price( $currency, $format ) { 
  48.  
  49. $wc_currencies = get_woocommerce_currencies(); 
  50. if( preg_match( '#%subtotal%#', $format ) ) { // include cart total 
  51. $cart_object =& $this->woocommerce_wpml->cart; 
  52. $wcml_settings = $this->woocommerce_wpml->get_settings(); 
  53. $multi_currency =& $this->woocommerce_wpml->multi_currency; 
  54. $client_currency = $multi_currency->get_client_currency(); 
  55.  
  56. if( preg_match( '#%subtotal%#', $format ) ) { // include cart total 
  57. if( !is_admin() ) { 
  58.  
  59. $multi_currency->set_client_currency( $currency ); 
  60. $cart_object->woocommerce_calculate_totals( WC()->cart, $currency ); 
  61. $cart_subtotal = WC()->cart->get_cart_subtotal(); 
  62.  
  63. }else{ 
  64. switch( $wcml_settings['currency_options'][$currency]['position'] ) { 
  65. case 'left' : 
  66. $price_format = '%1$s%2$s'; 
  67. break; 
  68. case 'right' : 
  69. $price_format = '%2$s%1$s'; 
  70. break; 
  71. case 'left_space' : 
  72. $price_format = '%1$s %2$s'; 
  73. break; 
  74. case 'right_space' : 
  75. $price_format = '%2$s %1$s'; 
  76. break; 
  77. $cart_subtotal = wc_price('1234.56',  
  78. array( 
  79. 'currency' => $currency,  
  80. 'decimal_separator' => $wcml_settings['currency_options'][$currency]['decimal_sep'],  
  81. 'thousand_separator' => $wcml_settings['currency_options'][$currency]['thousand_sep'],  
  82. 'decimals' => $wcml_settings['currency_options'][$currency]['num_decimals'],  
  83. 'price_format' => $price_format 
  84. ); 
  85. }else{ 
  86. $cart_subtotal = false; 
  87.  
  88. $currency_format = preg_replace( array('#%name%#', '#%symbol%#', '#%code%#', '#%subtotal%#'),  
  89. array( 
  90. $wc_currencies[$currency],  
  91. get_woocommerce_currency_symbol( $currency ),  
  92. $currency,  
  93. $cart_subtotal 
  94.  
  95. ), $format ); 
  96.  
  97. if( preg_match( '#%subtotal%#', $format ) && !is_admin() ) { // include cart total 
  98. $multi_currency->set_client_currency( $client_currency ); 
  99.  
  100. return $currency_format; 
  101.  
  102. public function render() { 
  103. echo $this->get_view(); 
  104.  
  105. protected function init_template_base_dir() { 
  106. $this->template_paths = array( 
  107. WCML_PLUGIN_PATH . '/templates/multi-currency/',  
  108. ); 
  109.  
  110. public function get_template() { 
  111. return 'currency-switcher.twig'; 
  112.  
  113.  
  114.