/inc/template-classes/multi-currency/class-wcml-currency-switcher-ui.php

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