8

How can I get current currency and currency symbol in Magento 2 in a template (.phtml file)? Without creating a block

3

8 Answers 8

20

please note that creating this with objectManager, not the best practice

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currency = $storeManager->getStore()->getCurrentCurrency();

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()->getCurrentCurrency(); 
    $currency = $objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currencyCode); 
    echo $currencySymbol = $currency->getCurrencySymbol(); ?>
3
  • 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(); ?> Commented Aug 8, 2016 at 9:21
  • Hi Arun, you should post your code as an answer ;)
    – 7ochem
    Commented Aug 8, 2016 at 9:24
  • Alternative to get the currency symbol: $this->storeManager->getStore()->getBaseCurrency()->getCurrencySymbol(); Commented Dec 4, 2021 at 21:56
14

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.

0
10

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();
1
  • Best Approach, Thank you! Commented Jun 9, 2022 at 10:58
6
<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterfa‌​ce'); 
$currencyCode = $storeManager->getStore()->getCurrentCurrencyCode(); 
$currency = $objectManager->create('\Magento\Directory\Model\CurrencyFact‌​ory')->create()->loa‌​d($currencyCode); 
echo $currencySymbol = $currency->getCurrencySymbol(); 
?>

Now you will not face the fatal error

2
  • 1
    Above code give me below error in phtml page:- Class Magento\Store\Model\StoreManagerInterfa‌​ce does not exist Commented Aug 30, 2017 at 4:52
  • The StoreManagerInterfa‌​ce contain Non-ASCII characters, so it has the Class Magento\Store\Model\StoreManagerInterfa‌​ce does not exist error, we need rewrite it to Magento\Store\Model\StoreManagerInterface. It is difficult to see the difference by eyes. @PurushotamSharma
    – Key Shang
    Commented Jun 20, 2018 at 3:53
2
$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();
1
public function __construct(
 ...
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 ... )
     {
    ...
    $this->_storeManager = $storeManager;
    ...
}

public function getCurrencySymbol() {
        return $this ->_storeManager-> getStore()->getBaseCurrency()->getCurrencySymbol();
    }
1

I have noticed that some of the answers above includes code with invisible characters (&#8203 and &#8204, 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();
-3

A shorter version that should work:

<?php 
    echo Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol(); 
?>
1
  • 1
    The requirement is for Magento-2 version. Your code belongs to Magento-1.
    – MGento
    Commented Nov 10, 2017 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.