I used below event
<event name="currency_display_options_forming">
<observer name="vendor_extension_change_currency_position" instance="Vendor\Module\Observer\ChangeCurrencySymbol" />
</event>
then,
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Locale\Currency;
use Magento\Directory\Model\CurrencyFactory;
class ChangeCurrencySymbol implements ObserverInterface
{
private $logger;
protected $symbolFactory;
protected $_coreSession;
private $currencyCode;
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $symbolFactory,
\Magento\Framework\Session\SessionManagerInterface $coreSession,
CurrencyFactory $currencyFactory
){
$this->logger = $logger;
$this->symbolFactory = $symbolFactory;
$this->_coreSession = $coreSession;
$this->currencyCode = $currencyFactory->create();
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$baseCode = $observer->getEvent()->getBaseCode();
$currencyOptions = $observer->getEvent()->getCurrencyOptions();
$currencyOptions->addData($this->getCurrencyOptions($baseCode));
return $this;
}
protected function getCurrencyOptions($baseCode)
{
$currencyCode = $this->getActiveCurrencyCode();
$currencyOptions = [];
if ($baseCode) {
//$customCurrencySymbol = $this->symbolFactory->create()->getCurrencySymbol($baseCode);
$currency = $this->currencyCode->load($currencyCode);
$currencySymbol = $currency->getCurrencySymbol();
$this->logger->info('currencySymbolObs'.$currencySymbol);
$this->logger->info('customCurrencySymbol'.$customCurrencySymbol);
if ($customCurrencySymbol) {
$currencyOptions[Currency::CURRENCY_OPTION_SYMBOL] = $customCurrencySymbol;
$currencyOptions[Currency::CURRENCY_OPTION_DISPLAY] = \Magento\Framework\Currency::USE_SYMBOL;
}
}
return $currencyOptions;
}
public function getActiveCurrencyCode(){
$this->_coreSession->start();
$sessionCurrency = $this->_coreSession->getActiveCurrency();
if(isset($sessionCurrency) && $sessionCurrency != ''){
return $sessionCurrency;
}else{
$sessionCurrency = GBP;
return $sessionCurrency;
}
}
}
I have drop down in header with countries, Once any country is selected, I am setting value(Currency Code) in session by calling custom controller.
$currencyCode = $this->getRequest()->getPostValue('currency_code');
$this->_coreSession->setActiveCurrency($currencyCode);
I am looking for code how can we show custom currency symbol in all the pages.
I need to get the currency Symbol from currency code and show that symbol everywhere where prices are displayed. getActiveCurrencyCode() function returns the currency code.
Above code is not working as expected, This is hitting page continuously and site is getting hanged.
Can anyone help me to achieve this functionality.
Is this approach is good to achieve that functionality? Please look into it and share your thoughts with better approach. Thanks!!