How can I get current currency and currency symbol in Magento 2 in a template (.phtml
file)? Without creating a block
-
2you may refer magento.stackexchange.com/questions/108012/… & blog.chapagain.com.np/… – Arjun Aug 8 '16 at 9:03
-
I have already checked the links both of them created block and used them I dont want to create block – Arun Karnawat Aug 8 '16 at 9:06
-
Use a block. Templates should not contain business logic that should be in blocks or models. – Daniel Ifrim Oct 21 '18 at 11:24
please note that creating this with objectManager, not the best practice
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currencysymbol = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currency = $currencysymbol->getStore()->getCurrentCurrencyCode();
getCurrentCurrencyCode() Gives the Currency code and not the symbol but you were helpful rest I figured -
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currencyCode = $storeManager->getStore()->getCurrentCurrencyCode();
$currency = $objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currencyCode);
echo $currencySymbol = $currency->getCurrencySymbol(); ?>
-
getCurrentCurrencyCode() Gives the Currency code and not the symbol but you was helpful rest I figured -
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); $currencyCode = $storeManager->getStore()->getCurrentCurrencyCode(); $currency = $objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currencyCode); echo $currencySymbol = $currency->getCurrencySymbol(); ?>
– Arun Karnawat Aug 8 '16 at 9:21 -
Another way to approach this would be to use the Pricing\Helper, this will both give you the currency symbol and format the number to the correct decimal place.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data'); // Instance of Pricing Helper
$value = 1000; // or whatever value you have in your template
$formattedCurrencyValue = $priceHelper->currency($value, true, false);
Although, I believe it's bad practice calling ObjectManager from within a template, for something as simple as getting the correct currency symbol you would imagine it would be more straight forward.
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$currencyCode = $storeManager->getStore()->getCurrentCurrencyCode();
$currency = $objectManager->create('\Magento\Directory\Model\CurrencyFactory')->create()->load($currencyCode);
echo $currencySymbol = $currency->getCurrencySymbol();
?>
Now you will not face the fatal error
-
1Above code give me below error in phtml page:-
Class Magento\Store\Model\StoreManagerInterface does not exist
– Purushotam Sharma Aug 30 '17 at 4:52 -
The
StoreManagerInterface
contain Non-ASCII characters, so it has theClass Magento\Store\Model\StoreManagerInterface does not exist
error, we need rewrite it toMagento\Store\Model\StoreManagerInterface
. It is difficult to see the difference by eyes. @PurushotamSharma – Key Shang Jun 20 '18 at 3:53
create helper file with method:
I believe it's right practice
use Magento\Store\Model\StoreManagerInterface;
use Magento\Directory\Model\CurrencyFactory;
class Data
{
/**
* @var StoreManagerInterface
*/
private $storeConfig;
/**
* @var CurrencyFactory
*/
private $currencyCode;
/**
* Currency constructor.
*
* @param StoreManagerInterface $storeConfig
* @param CurrencyFactory $currencyFactory
*/
public function __construct(
StoreManagerInterface $storeConfig,
CurrencyFactory $currencyFactory
) {
$this->storeConfig = $storeConfig;
$this->currencyCode = $currencyFactory->create();
}
/**
* @return string
*/
public function getSymbol()
{
$currentCurrency = $this->storeConfig->getStore()->getCurrentCurrencyCode();
$currency = $this->currencyCode->load($currentCurrency);
return $currency->getCurrencySymbol();
}
}
call helper file in phtml
$this->helper('YourNamespace\Modulename\Helper\Data')->getSymbol();
public function __construct(
...
\Magento\Store\Model\StoreManagerInterface $storeManager,
... )
{
...
$this->_storeManager = $storeManager;
...
}
public function getCurrencySymbol() {
return $this ->_storeManager-> getStore()->getBaseCurrency()->getCurrencySymbol();
}
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currencysymbol = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currency = $currencysymbol->getStore()->getCurrentCurrencyCode();
$currency = $objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currency);
$currencySymbol = $currency->getCurrencySymbol();
I have noticed that some of the answers above includes code with invisible characters (​ and ‌, in particular) that makes it unusable. I haven't tried all versions of the code that has been posted here, since many have posted the same code. But here comes a clean code, with no invisible characters.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currencyFactory = $objectManager->create('Magento\Directory\Model\CurrencyFactory');
$currencyCode = $storeManager->getStore()->getCurrentCurrencyCode();
$currency = $currencyFactory->create()->load($currencyCode);
$currencySymbol = $currency->getCurrencySymbol();
A shorter version that should work:
<?php
echo Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
?>
-
1The requirement is for Magento-2 version. Your code belongs to Magento-1. – MGento Nov 10 '17 at 4:23