Show custom currency symbol in Magento2main.CRITICAL: Plugin class doesn't existSet custom price of product when adding to cart code not workingWhy Getting categories and names on product view page Magento 2 fails?Magento 2: Add a product to the cart programmaticallyMagento 2.3 email attachment not working while sending custom email
Handling Disruptive Student on the Autism Spectrum
Photoshop: How can I change the layer type?
Ghidra: Prepend memory segment in assembly listing view
What should come first—characters or plot?
Do Bayesian credible intervals treat the estimated parameter as a random variable?
Was the Boeing 2707 design flawed?
Why is the UK so keen to remove the "backstop" when their leadership seems to think that no border will be needed in Northern Ireland?
Command "root" and "subcommands"
Separating old 2 x 4 brick with wheel holder
Why isn't "I've" a proper response?
"Opusculum hoc, quamdiu vixero, doctioribus emendandum offero."?
Does maintaining a spell with a longer casting time count as casting a spell?
How to gently end involvement with an online community?
How do I make my image comply with the requirements of this photography competition?
Non-visual Computers - thoughts?
Why do banks “park” their money at the European Central Bank?
Foreign language movie, people enter a church but find they can't leave
Boot Windows from SAN
Another solution to create a set with two conditions
Filling a listlineplot with a texture
Limitations with dynamical systems vs. PDEs?
How do proponents of Sola Scriptura address the ministry of those Apostles who authored no parts of Scripture?
What does "rel" in `mathrel` and `stackrel` stands for?
Does ostensible/specious make sense in this sentence?
Show custom currency symbol in Magento2
main.CRITICAL: Plugin class doesn't existSet custom price of product when adding to cart code not workingWhy Getting categories and names on product view page Magento 2 fails?Magento 2: Add a product to the cart programmaticallyMagento 2.3 email attachment not working while sending custom email
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I used below event
<event name="currency_display_options_forming">
<observer name="vendor_extension_change_currency_position" instance="VendorModuleObserverChangeCurrencySymbol" />
</event>
then,
<?php
namespace VendorModuleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkLocaleCurrency;
use MagentoDirectoryModelCurrencyFactory;
class ChangeCurrencySymbol implements ObserverInterface
private $logger;
protected $symbolFactory;
protected $_coreSession;
private $currencyCode;
public function __construct(
PsrLogLoggerInterface $logger,
MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory,
MagentoFrameworkSessionSessionManagerInterface $coreSession,
CurrencyFactory $currencyFactory
)
$this->logger = $logger;
$this->symbolFactory = $symbolFactory;
$this->_coreSession = $coreSession;
$this->currencyCode = $currencyFactory->create();
public function execute(MagentoFrameworkEventObserver $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] = MagentoFrameworkCurrency::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!!
magento2 currency currency-symbol
add a comment |
I used below event
<event name="currency_display_options_forming">
<observer name="vendor_extension_change_currency_position" instance="VendorModuleObserverChangeCurrencySymbol" />
</event>
then,
<?php
namespace VendorModuleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkLocaleCurrency;
use MagentoDirectoryModelCurrencyFactory;
class ChangeCurrencySymbol implements ObserverInterface
private $logger;
protected $symbolFactory;
protected $_coreSession;
private $currencyCode;
public function __construct(
PsrLogLoggerInterface $logger,
MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory,
MagentoFrameworkSessionSessionManagerInterface $coreSession,
CurrencyFactory $currencyFactory
)
$this->logger = $logger;
$this->symbolFactory = $symbolFactory;
$this->_coreSession = $coreSession;
$this->currencyCode = $currencyFactory->create();
public function execute(MagentoFrameworkEventObserver $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] = MagentoFrameworkCurrency::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!!
magento2 currency currency-symbol
add a comment |
I used below event
<event name="currency_display_options_forming">
<observer name="vendor_extension_change_currency_position" instance="VendorModuleObserverChangeCurrencySymbol" />
</event>
then,
<?php
namespace VendorModuleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkLocaleCurrency;
use MagentoDirectoryModelCurrencyFactory;
class ChangeCurrencySymbol implements ObserverInterface
private $logger;
protected $symbolFactory;
protected $_coreSession;
private $currencyCode;
public function __construct(
PsrLogLoggerInterface $logger,
MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory,
MagentoFrameworkSessionSessionManagerInterface $coreSession,
CurrencyFactory $currencyFactory
)
$this->logger = $logger;
$this->symbolFactory = $symbolFactory;
$this->_coreSession = $coreSession;
$this->currencyCode = $currencyFactory->create();
public function execute(MagentoFrameworkEventObserver $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] = MagentoFrameworkCurrency::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!!
magento2 currency currency-symbol
I used below event
<event name="currency_display_options_forming">
<observer name="vendor_extension_change_currency_position" instance="VendorModuleObserverChangeCurrencySymbol" />
</event>
then,
<?php
namespace VendorModuleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkLocaleCurrency;
use MagentoDirectoryModelCurrencyFactory;
class ChangeCurrencySymbol implements ObserverInterface
private $logger;
protected $symbolFactory;
protected $_coreSession;
private $currencyCode;
public function __construct(
PsrLogLoggerInterface $logger,
MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory,
MagentoFrameworkSessionSessionManagerInterface $coreSession,
CurrencyFactory $currencyFactory
)
$this->logger = $logger;
$this->symbolFactory = $symbolFactory;
$this->_coreSession = $coreSession;
$this->currencyCode = $currencyFactory->create();
public function execute(MagentoFrameworkEventObserver $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] = MagentoFrameworkCurrency::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!!
magento2 currency currency-symbol
magento2 currency currency-symbol
edited Aug 16 at 9:55
jafar pinjar
asked Aug 13 at 15:47
jafar pinjarjafar pinjar
1,0375 silver badges27 bronze badges
1,0375 silver badges27 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 VendorModuleObserver;
use MagentoFrameworkLocaleCurrency;
use MagentoFrameworkEventObserverInterface;
class CurrencyDisplayOptions implements ObserverInterface
/**
* @var MagentoCurrencySymbolModelSystemCurrencysymbolFactory
*/
protected $symbolFactory;
/**
* @param MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory
*/
public function __construct(MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory)
$this->symbolFactory = $symbolFactory;
/**
* Generate options for currency displaying with custom currency symbol
*
* @param MagentoFrameworkEventObserver $observer
* @return $this
*/
public function execute(MagentoFrameworkEventObserver $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] = MagentoFrameworkCurrency::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.
hi @Rohan, this code shows my custom symbol where symbol is showing currently?
– jafar pinjar
Aug 14 at 6:31
1
Hi @Rohan, Great!! its working
– jafar pinjar
Aug 14 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
– jafar pinjar
Aug 14 at 12:35
Using country code it will not working. You should pass currency code in that.
– Rohan Hapani
Aug 15 at 8:53
Yes, sorry Iam passing currency code only
– jafar pinjar
Aug 15 at 12:53
|
show 3 more comments
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f285369%2fshow-custom-currency-symbol-in-magento2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 VendorModuleObserver;
use MagentoFrameworkLocaleCurrency;
use MagentoFrameworkEventObserverInterface;
class CurrencyDisplayOptions implements ObserverInterface
/**
* @var MagentoCurrencySymbolModelSystemCurrencysymbolFactory
*/
protected $symbolFactory;
/**
* @param MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory
*/
public function __construct(MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory)
$this->symbolFactory = $symbolFactory;
/**
* Generate options for currency displaying with custom currency symbol
*
* @param MagentoFrameworkEventObserver $observer
* @return $this
*/
public function execute(MagentoFrameworkEventObserver $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] = MagentoFrameworkCurrency::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.
hi @Rohan, this code shows my custom symbol where symbol is showing currently?
– jafar pinjar
Aug 14 at 6:31
1
Hi @Rohan, Great!! its working
– jafar pinjar
Aug 14 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
– jafar pinjar
Aug 14 at 12:35
Using country code it will not working. You should pass currency code in that.
– Rohan Hapani
Aug 15 at 8:53
Yes, sorry Iam passing currency code only
– jafar pinjar
Aug 15 at 12:53
|
show 3 more comments
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 VendorModuleObserver;
use MagentoFrameworkLocaleCurrency;
use MagentoFrameworkEventObserverInterface;
class CurrencyDisplayOptions implements ObserverInterface
/**
* @var MagentoCurrencySymbolModelSystemCurrencysymbolFactory
*/
protected $symbolFactory;
/**
* @param MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory
*/
public function __construct(MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory)
$this->symbolFactory = $symbolFactory;
/**
* Generate options for currency displaying with custom currency symbol
*
* @param MagentoFrameworkEventObserver $observer
* @return $this
*/
public function execute(MagentoFrameworkEventObserver $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] = MagentoFrameworkCurrency::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.
hi @Rohan, this code shows my custom symbol where symbol is showing currently?
– jafar pinjar
Aug 14 at 6:31
1
Hi @Rohan, Great!! its working
– jafar pinjar
Aug 14 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
– jafar pinjar
Aug 14 at 12:35
Using country code it will not working. You should pass currency code in that.
– Rohan Hapani
Aug 15 at 8:53
Yes, sorry Iam passing currency code only
– jafar pinjar
Aug 15 at 12:53
|
show 3 more comments
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 VendorModuleObserver;
use MagentoFrameworkLocaleCurrency;
use MagentoFrameworkEventObserverInterface;
class CurrencyDisplayOptions implements ObserverInterface
/**
* @var MagentoCurrencySymbolModelSystemCurrencysymbolFactory
*/
protected $symbolFactory;
/**
* @param MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory
*/
public function __construct(MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory)
$this->symbolFactory = $symbolFactory;
/**
* Generate options for currency displaying with custom currency symbol
*
* @param MagentoFrameworkEventObserver $observer
* @return $this
*/
public function execute(MagentoFrameworkEventObserver $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] = MagentoFrameworkCurrency::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.
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 VendorModuleObserver;
use MagentoFrameworkLocaleCurrency;
use MagentoFrameworkEventObserverInterface;
class CurrencyDisplayOptions implements ObserverInterface
/**
* @var MagentoCurrencySymbolModelSystemCurrencysymbolFactory
*/
protected $symbolFactory;
/**
* @param MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory
*/
public function __construct(MagentoCurrencySymbolModelSystemCurrencysymbolFactory $symbolFactory)
$this->symbolFactory = $symbolFactory;
/**
* Generate options for currency displaying with custom currency symbol
*
* @param MagentoFrameworkEventObserver $observer
* @return $this
*/
public function execute(MagentoFrameworkEventObserver $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] = MagentoFrameworkCurrency::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.
edited Aug 16 at 10:57
answered Aug 14 at 4:50
Rohan HapaniRohan Hapani
8,8984 gold badges21 silver badges66 bronze badges
8,8984 gold badges21 silver badges66 bronze badges
hi @Rohan, this code shows my custom symbol where symbol is showing currently?
– jafar pinjar
Aug 14 at 6:31
1
Hi @Rohan, Great!! its working
– jafar pinjar
Aug 14 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
– jafar pinjar
Aug 14 at 12:35
Using country code it will not working. You should pass currency code in that.
– Rohan Hapani
Aug 15 at 8:53
Yes, sorry Iam passing currency code only
– jafar pinjar
Aug 15 at 12:53
|
show 3 more comments
hi @Rohan, this code shows my custom symbol where symbol is showing currently?
– jafar pinjar
Aug 14 at 6:31
1
Hi @Rohan, Great!! its working
– jafar pinjar
Aug 14 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
– jafar pinjar
Aug 14 at 12:35
Using country code it will not working. You should pass currency code in that.
– Rohan Hapani
Aug 15 at 8:53
Yes, sorry Iam passing currency code only
– jafar pinjar
Aug 15 at 12:53
hi @Rohan, this code shows my custom symbol where symbol is showing currently?
– jafar pinjar
Aug 14 at 6:31
hi @Rohan, this code shows my custom symbol where symbol is showing currently?
– jafar pinjar
Aug 14 at 6:31
1
1
Hi @Rohan, Great!! its working
– jafar pinjar
Aug 14 at 7:14
Hi @Rohan, Great!! its working
– jafar pinjar
Aug 14 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
– jafar pinjar
Aug 14 at 12:35
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
– jafar pinjar
Aug 14 at 12:35
Using country code it will not working. You should pass currency code in that.
– Rohan Hapani
Aug 15 at 8:53
Using country code it will not working. You should pass currency code in that.
– Rohan Hapani
Aug 15 at 8:53
Yes, sorry Iam passing currency code only
– jafar pinjar
Aug 15 at 12:53
Yes, sorry Iam passing currency code only
– jafar pinjar
Aug 15 at 12:53
|
show 3 more comments
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f285369%2fshow-custom-currency-symbol-in-magento2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown