4

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

5
$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 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(); ?>
  • 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
  • Hi Arun, you should post your code as an answer ;) – 7ochem Aug 8 '16 at 9:24
10

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.

  • Thank u very much Holly its working for me. – Nagaraju Kasa Jul 14 '17 at 10:28
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

  • 1
    Above code give me below error in phtml page:- Class Magento\Store\Model\StoreManagerInterfa‌​ce does not exist – Purushotam Sharma Aug 30 '17 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 Jun 20 '18 at 3:53
2

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
public function __construct(
 ...
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 ... )
     {
    ...
    $this->_storeManager = $storeManager;
    ...
}

public function getCurrencySymbol() {
        return $this ->_storeManager-> getStore()->getBaseCurrency()->getCurrencySymbol();
    }
1
$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();
0

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();
-2

A shorter version that should work:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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