Пожалуйста, помогите мне получить код для распечатки общей суммы с символом валюты в файле .phtml в Magento2.Получить текущий символ валюты в Magento2
ответ
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();
}
Попробуйте этот код.
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currencysymbol = $objectManager->get('Magento\Directory\Model\Currency');
echo $currencysymbol->getCurrencySymbol();
?>
Используя диспетчер объектов непосредственно, чтобы получить объекты неодобрением, не так ли? –
Да, вместо этого используйте вместо – javiervd
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);
Пожалуйста, добавьте код, или показать нам, что вы искали до сих пор – Nehal