Currency.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Currency dropdown block
  8. */
  9. namespace Magento\Directory\Block;
  10. use Magento\Framework\Locale\Bundle\CurrencyBundle as CurrencyBundle;
  11. /**
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Currency extends \Magento\Framework\View\Element\Template
  16. {
  17. /**
  18. * @var \Magento\Directory\Model\CurrencyFactory
  19. */
  20. protected $_currencyFactory;
  21. /**
  22. * @var \Magento\Framework\Data\Helper\PostHelper
  23. */
  24. protected $_postDataHelper;
  25. /**
  26. * @var \Magento\Framework\Locale\ResolverInterface
  27. */
  28. protected $localeResolver;
  29. /**
  30. * @param \Magento\Framework\View\Element\Template\Context $context
  31. * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
  32. * @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
  33. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  34. * @param array $data
  35. */
  36. public function __construct(
  37. \Magento\Framework\View\Element\Template\Context $context,
  38. \Magento\Directory\Model\CurrencyFactory $currencyFactory,
  39. \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
  40. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  41. array $data = []
  42. ) {
  43. $this->_currencyFactory = $currencyFactory;
  44. $this->_postDataHelper = $postDataHelper;
  45. parent::__construct($context, $data);
  46. $this->localeResolver = $localeResolver;
  47. }
  48. /**
  49. * Retrieve count of currencies
  50. * Return 0 if only one currency
  51. *
  52. * @return int
  53. */
  54. public function getCurrencyCount()
  55. {
  56. return count($this->getCurrencies());
  57. }
  58. /**
  59. * Retrieve currencies array
  60. * Return array: code => currency name
  61. * Return empty array if only one currency
  62. *
  63. * @return array
  64. */
  65. public function getCurrencies()
  66. {
  67. $currencies = $this->getData('currencies');
  68. if ($currencies === null) {
  69. $currencies = [];
  70. $codes = $this->_storeManager->getStore()->getAvailableCurrencyCodes(true);
  71. if (is_array($codes) && count($codes) > 1) {
  72. $rates = $this->_currencyFactory->create()->getCurrencyRates(
  73. $this->_storeManager->getStore()->getBaseCurrency(),
  74. $codes
  75. );
  76. foreach ($codes as $code) {
  77. if (isset($rates[$code])) {
  78. $allCurrencies = (new CurrencyBundle())->get(
  79. $this->localeResolver->getLocale()
  80. )['Currencies'];
  81. $currencies[$code] = $allCurrencies[$code][1] ?: $code;
  82. }
  83. }
  84. }
  85. $this->setData('currencies', $currencies);
  86. }
  87. return $currencies;
  88. }
  89. /**
  90. * Retrieve Currency Switch URL
  91. *
  92. * @return string
  93. */
  94. public function getSwitchUrl()
  95. {
  96. return $this->getUrl('directory/currency/switch');
  97. }
  98. /**
  99. * Return POST data for currency to switch
  100. *
  101. * @param string $code
  102. * @return string
  103. */
  104. public function getSwitchCurrencyPostData($code)
  105. {
  106. return $this->_postDataHelper->getPostData($this->escapeUrl($this->getSwitchUrl()), ['currency' => $code]);
  107. }
  108. /**
  109. * Retrieve Current Currency code
  110. *
  111. * @return string
  112. */
  113. public function getCurrentCurrencyCode()
  114. {
  115. if ($this->_getData('current_currency_code') === null) {
  116. // do not use $this->_storeManager->getStore()->getCurrentCurrencyCode() because of probability
  117. // to get an invalid (without base rate) currency from code saved in session
  118. $this->setData('current_currency_code', $this->_storeManager->getStore()->getCurrentCurrency()->getCode());
  119. }
  120. return $this->_getData('current_currency_code');
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function getStoreCode()
  126. {
  127. return $this->_storeManager->getStore()->getCode();
  128. }
  129. }