4

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!!

2 Answers 2

5
+50

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.

12
  • hi @Rohan, this code shows my custom symbol where symbol is showing currently? Aug 14, 2019 at 6:31
  • 1
    Hi @Rohan, Great!! its working Aug 14, 2019 at 7:14
  • This has some issue, in place of this line, $customCurrencySymbol = $this->symbolFactory->create()->getCurrencySymbol($baseCode); I have written function to get symbol based on country code, that is not working Aug 14, 2019 at 12:35
  • Using country code it will not working. You should pass currency code in that. Aug 15, 2019 at 8:53
  • Yes, sorry Iam passing currency code only Aug 15, 2019 at 12:53
0

Another method would be to use "after plugin" and implement function afterGetCurrency

Here's a sample of the

di.xml

<type name="Magento\Framework\Locale\Currency">
<plugin disabled="false" name="Sample_Module::customcurrency" sortOrder="1" type="Sample\Module\Plugin\Magento\Framework\Locale\Currency" />
</type>

plugin class

namespace Sample\Module\Plugin\Magento\Framework\Locale;

class Currency
{
    /**
     * @param \Magento\Framework\Locale\Currency $subject
     * @param \Magento\Framework\Currency $result
     * @return mixed
     */
    public function afterGetCurrency(
        \Magento\Framework\Locale\Currency $subject,
        $result,
        $currency
    ) {
        //Your plugin code
        return $result;
    }
}
2
  • can you upload me full answer Sep 23, 2019 at 6:41
  • Updated the answer
    – Ocastro
    Sep 24, 2019 at 3:25

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.