Magento – Magento 2 use symbol instead of name in currency switcher

magento2

I am trying to figure out how to get the currency symbol instead of the name on the currency switcher in Magento 2. I have done it with php, but I know there is a better way in Magento.

I've created a new file: /app/design/frontend/<Vendor>/theme/Magento_Directory/templates/currency.phtml.

I also added a coin image.

<?php
/**
 * @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 */
?>
<?php
/**
 * Currency switcher
 *
 * @see \Magento\Directory\Block\Currency
 */
?>
<?php if ($this->getCurrencyCount() > 1): ?>
<?php $currencies = $this->getCurrencies(); ?>
<?php $currentCurrencyCode = $this->getCurrentCurrencyCode(); ?>
<?php $id = $this->getIdModifier() ? '-' . $this->getIdModifier() : ''?>
<div class="switcher currency switcher-currency" id="switcher-currency<?php echo $id?>">
    <strong class="label switcher-label"><span><?php echo __('Currency') ?></span></strong>
    <div class="actions dropdown options switcher-options">
        <div class="action toggle switcher-trigger" id="switcher-currency-trigger<?php echo $id?>">
            <strong class="language-<?php echo $this->escapeHtml($this->getCurrentCurrencyCode()) ?>">
                <span><img src="<?php echo $this->getViewFileUrl('images/coin.png') ?>" alt="currency" /> <?php //echo $this->escapeHtml($currentCurrencyCode) ?><?php
                            $locale='en-AU'; //browser or user locale
                            $currency= $this->escapeHtml($this->getCurrentCurrencyCode());
                            $fmt = new NumberFormatter( $locale."@currency=$currency", NumberFormatter::CURRENCY );
                            $symbol = $fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
                            header("Content-Type: text/html; charset=UTF-8;");
                            echo $symbol;
                            ?></span>
            </strong>
        </div>
        <ul class="dropdown switcher-dropdown" data-mage-init='{"dropdownDialog":{
            "appendTo":"#switcher-currency<?php echo $id?> > .options",
            "triggerTarget":"#switcher-currency-trigger<?php echo $id?>",
            "closeOnMouseLeave": false,
            "triggerClass":"active",
            "parentClass":"active",
            "buttons":null}}'>
            <?php foreach ($currencies as $_code => $_name): ?>
                <?php if ($_code != $currentCurrencyCode): ?>
                    <li class="currency-<?php echo $_code ?> switcher-option">
                        <a href="#" data-post='<?php echo $this->getSwitchCurrencyPostData($_code); ?>'><?php echo $_code ?> - <?php
                            $locale='en-AU'; //browser or user locale
                            $currency= $_code;
                            $fmt = new NumberFormatter( $locale."@currency=$currency", NumberFormatter::CURRENCY );
                            $symbol = $fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
                            header("Content-Type: text/html; charset=UTF-8;");
                            echo $symbol;
                            ?>
                        </a>
                    </li>
                <?php endif; ?>
            <?php endforeach; ?>
        </ul>
    </div>
</div>
<?php endif; ?>

What is the best way to get magento currency symbol using magento way?

Best Answer

To Get Currency Symbol

$currencies = $block->getCurrencies(); 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

$currencyManager = $objectManager->create('Magento\Directory\Model\PriceCurrency');

foreach ($currencies as $_code => $_name){
    echo $currencySymbol = $currencyManager->getCurrencySymbol(null,$_code);
}
Related Topic