在Magento2中获取当前货币符号

时间:2015-12-18 05:49:21

标签: magento currency symbols magento2

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

5 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:2)

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

答案 2 :(得分:1)

试试这段代码。

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

    echo $currencysymbol->getCurrencySymbol();
?>

答案 3 :(得分:0)

您可以使用以下代码:

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

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

答案 4 :(得分:-1)

获取货币符号:

let currentWrapper = [...serviceResultsWrapper].find((wrapper) => wrapper.dataset.stationcode === currentStationCode);
let serviceBoxFragment = document.createRange().createContextualFragment(serviceBoxTemplate);

currentWrapper.appendChild(serviceBoxFragment);