Show custom currency symbol in Magento2
You need add "symbol" in $currencyOptions
array. Please add this below code in your observer file :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Observer;
use Magento\Framework\Locale\Currency;
use Magento\Framework\Event\ObserverInterface;
class CurrencyDisplayOptions implements ObserverInterface
{
/**
* @var \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory
*/
protected $symbolFactory;
/**
* @param \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $symbolFactory
*/
public function __construct(\Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $symbolFactory)
{
$this->symbolFactory = $symbolFactory;
}
/**
* Generate options for currency displaying with custom currency symbol
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$baseCode = $observer->getEvent()->getBaseCode();
$currencyOptions = $observer->getEvent()->getCurrencyOptions();
$currencyOptions->addData($this->getCurrencyOptions($baseCode));
return $this;
}
/**
* Get currency display options
*
* @param string $baseCode
* @return array
*/
protected function getCurrencyOptions($baseCode)
{
$currencyOptions = [];
if ($baseCode) {
$customCurrencySymbol = $this->symbolFactory->create()->getCurrencySymbol($baseCode);
if ($customCurrencySymbol) {
$currencyOptions[Currency::CURRENCY_OPTION_SYMBOL] = $customCurrencySymbol;
$currencyOptions[Currency::CURRENCY_OPTION_DISPLAY] = \Magento\Framework\Currency::USE_SYMBOL;
}
}
return $currencyOptions;
}
}
UPDATED :
You loaded every time currency object using this below line and create object in construct. You should remove this below lines :
$this->currencyCode = $currencyFactory->create(); // Remove this class from construct
$currency = $this->currencyCode->load($currencyCode);
$currencySymbol = $currency->getCurrencySymbol();
and use this below line for get currency symbol :
$customCurrencySymbol = $this->symbolFactory->create()->getCurrencySymbol($baseCode);
Remove generated folder and clean cache.
Hope, It will helpful for you.