在 Magento2 中获取当前货币符号

Get current currency symbol in Magento2

如何在 Magento2 的 .phtml 文件中编写用于打印带有货币符号的购物车总计的代码?

试试这个代码。

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

    echo $currencysymbol->getCurrencySymbol();
?>
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();
}
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;
}

这对我来说非常有效,可以获取当前货币符号

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();
}

您可以使用以下代码:

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

但这不是标准方法。需要注入Magento\Directory\Model\Currency然后使用

获取货币符号:

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

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

$price =  1000; //Your Price

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