Вопрос

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

Это было полезно?

Решение

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.

Другие советы

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;
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top