Magento 2: Get Currency Code, Currency Symbol & Currency Rate
Tis article shows how we can get our store’s currency code, currency symbol, and currency rate in Magento 2. We will be fetching all base, default and current currency code. We will also be fetching available currency codes and allowed currency codes.
Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object ofStoreManagerInterface & Currency in the constructor of my module’s block class.
app/code/Chapagain/HelloWorld/Block/HelloWorld.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $_storeManager;
protected $_currency;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Directory\Model\Currency $currency,
array $data = []
)
{
$this->_storeManager = $storeManager;
$this->_currency = $currency;
parent::__construct($context, $data);
}
/**
* Get store base currency code
*
* @return string
*/
public function getBaseCurrencyCode()
{
return $this->_storeManager->getStore()->getBaseCurrencyCode();
}
/**
* Get current store currency code
*
* @return string
*/
public function getCurrentCurrencyCode()
{
return $this->_storeManager->getStore()->getCurrentCurrencyCode();
}
/**
* Get default store currency code
*
* @return string
*/
public function getDefaultCurrencyCode()
{
return $this->_storeManager->getStore()->getDefaultCurrencyCode();
}
/**
* Get allowed store currency codes
*
* If base currency is not allowed in current website config scope,
* then it can be disabled with $skipBaseNotAllowed
*
* @param bool $skipBaseNotAllowed
* @return array
*/
public function getAvailableCurrencyCodes($skipBaseNotAllowed = false)
{
return $this->_storeManager->getStore()->getAvailableCurrencyCodes($skipBaseNotAllowed);
}
/**
* Get array of installed currencies for the scope
*
* @return array
*/
public function getAllowedCurrencies()
{
return $this->_storeManager->getStore()->getAllowedCurrencies();
}
/**
* Get current currency rate
*
* @return float
*/
public function getCurrentCurrencyRate()
{
return $this->_storeManager->getStore()->getCurrentCurrencyRate();
}
/**
* Get currency symbol for current locale and currency code
*
* @return string
*/
public function getCurrentCurrencySymbol()
{
return $this->_currency->getCurrencySymbol();
}
}
?>
|
See more functions in vendor/magento/module-store/Model/Store.php and vendor/magento/module-directory/Model/Currency.php.
Now, we print the currency rate, currency code, and currency symbol in our template (.phtml) file.
1
2
3
4
5
6
7
8
|
echo $block->getCurrentCurrencySymbol() . ‘<br />’;
echo $block->getCurrentCurrencyCode() . ‘<br />’;
echo $block->getBaseCurrencyCode() . ‘<br />’;
echo $block->getDefaultCurrencyCode() . ‘<br />’;
echo $block->getCurrentCurrencyRate() . ‘<br />’;
print_r($block->getAvailableCurrencyCodes()) . ‘<br />’;
print_r($block->getAllowedCurrencies()) . ‘<br />’;
|