2015-12-18 15 views

ответ

7
protected $_currency; 

public function __construct(
    \Magento\Backend\Block\Template\Context $context,  
    \Magento\Directory\Model\Currency $currency,   
    array $data = [] 
) 
{   
    $this->_currency = $currency;  
    parent::__construct($context, $data); 
} 

/** 
* Get currency symbol for current locale and currency code 
* 
* @return string 
*/ 
public function getCurrentCurrencySymbol() 
{ 
    return $this->_currency->getCurrencySymbol(); 
} 
0

Попробуйте этот код.

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $currencysymbol = $objectManager->get('Magento\Directory\Model\Currency'); 

    echo $currencysymbol->getCurrencySymbol(); 
?> 
+0

Используя диспетчер объектов непосредственно, чтобы получить объекты неодобрением, не так ли? –

+0

Да, вместо этого используйте вместо – javiervd

1
use Magento\Store\Model\StoreManagerInterface; 
use Magento\Framework\Locale\CurrencyInterface; 
/** 
* @var StoreManagerInterface 
*/ 
protected $_modelStoreManagerInterface; 
/** 
* @var \Magento\Framework\Locale\CurrencyInterface 
*/ 
protected $_localeCurrency; 
public function __construct(
    \Magento\Backend\Block\Template\Context $context,  
    StoreManagerInterface $modelStoreManagerInterface, 
    CurrencyInterface $localeCurrency,  
    array $data = [] 
) 
{   
    $this->_modelStoreManagerInterface = $modelStoreManagerInterface; 
    $this->_localeCurrency = $localeCurrency; 
    parent::__construct($context, $data); 
} 

/** 
* Get currency symbol for current locale and currency code 
* 
* @return string 
*/ 
public function getCurrentCurrencySymbol() 
{ 
    $currencyCode = $this->_modelStoreManagerInterface->getStore()->getBaseCurrencyCode(); 
    $currencySymbol = $this->_localeCurrency->getCurrency($currencyCode)->getSymbol(); 
    return $currencySymbol; 
} 
0

Это то, что действительно работает для меня, чтобы получить текущий валютный символ:

protected $_priceCurrency; 

public function __construct(
    \Magento\Backend\Block\Template\Context $context, 
    \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, 
    array $data = [] 
) 
{   
    $this->_priceCurrency = $priceCurrency; 
    parent::__construct($context, $data); 
} 

/** 
* Get current currency symbol 
* 
* @return string 
*/ 
public function getCurrentCurrencySymbol() 
{ 
    return $this->_priceCurrency->getCurrency()->getCurrencySymbol(); 
} 

Другой метод может быть следующим

protected $_localeCurrency; 

protected $_storeManager; 

public function __construct(
    \Magento\Backend\Block\Template\Context $context, 
    \Magento\Framework\Locale\CurrencyInterface $localeCurrency, 
    \Magento\Store\Model\StoreManagerInterface $storeManager, 
    array $data = [] 
) 
{   
    $this->_localeCurrency = $localeCurrency; 
    $this->_storeManager = $storeManager; 
    parent::__construct($context, $data); 
} 

/** 
* Get current currency symbol 
* 
* @return string 
*/ 
public function getCurrentCurrencySymbol() 
{ 
    $code = $this->_storeManager->getStore()->getCurrentCurrencyCode(); 
    return $this->_localeCurrency->getCurrency($code)->getSymbol(); 
} 
1

Вы можете использовать следующий код:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$currencysymbol = $objectManager->get('Magento\Directory\Model\Currency'); 
echo $currencysymbol->getCurrencySymbol(); 

Но это не стандартный метод. Вам необходимо ввести Magento \ Directory \ Model \ Currency, а затем использовать его.

0

Чтобы получить символ валюты:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data'); 

$price = 1000; //Your Price 

$formattedPrice = $priceHelper->currency($price, true, false);