Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento 2获取货币数据中的代码,费率,符号

今天我将向您展示如何获取Magento 2中的代码,速率和符号等货币数据。当您使用Magento 2平台构建电子商店时,您可以接受许多货币,这取决于目标客户。使用本指南,不仅可以清楚地获取默认和当前货币代码,还可以获取可用的货币代码和允许的货币代码。

概观

  • 第1步:声明 Mageplaza_HelloWorld
  • 第2步:获取phtml文件中货币数据的输出

第1步:声明 Mageplaza_HelloWorld

您将使用模块的块类Mageplaza_HelloWorld,然后可能StoreManagerInterface & Currency在模块的块类的构造函数中注入类的对象。

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;
    protected $_currency;
    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Directory\Model\Currency $currency,        
        array $data = []
    )
    {        
        $this->_storeManager = $storeManager;
        $this->_currency = $currency;        
        parent::__construct($context, $data);
    }
    
        
    /**
     * Get store base currency code
     *
     * @return string
     */
    public function getBaseCurrencyCode()
    {
        return $this->_storeManager->getStore()->getBaseCurrencyCode();
    }
    
     /**
     * Get current store currency code
     *
     * @return string
     */
    public function getCurrentCurrencyCode()
    {
        return $this->_storeManager->getStore()->getCurrentCurrencyCode();
    }    
    
    /**
     * Get default store currency code
     *
     * @return string
     */
    public function getDefaultCurrencyCode()
    {
        return $this->_storeManager->getStore()->getDefaultCurrencyCode();
    }
    
    /**
     * Get allowed store currency codes
     *
     * If base currency is not allowed in current website config scope,
     * then it can be disabled with $skipBaseNotAllowed
     *
     * @param bool $skipBaseNotAllowed
     * @return array
     */
    public function getAvailableCurrencyCodes($skipBaseNotAllowed = false)
    {
        return $this->_storeManager->getStore()->getAvailableCurrencyCodes($skipBaseNotAllowed);
    }
    
    /**
     * Get array of installed currencies for the scope
     *
     * @return array
     */
    public function getAllowedCurrencies()
    {
        return $this->_storeManager->getStore()->getAllowedCurrencies();
    }
    
    /**
     * Get current currency rate
     *
     * @return float
     */
    public function getCurrentCurrencyRate()
    {
        return $this->_storeManager->getStore()->getCurrentCurrencyRate();
    }
    
    /**
     * Get currency symbol for current locale and currency code
     *
     * @return string
     */    
    public function getCurrentCurrencySymbol()
    {
        return $this->_currency->getCurrencySymbol();
    }    
}
?>

你可以在vendor/magento/module-store/Model/Store.php和中看到更多功能vendor/magento/module-directory/Model/Currency.php

第2步:获取phtml文件中货币数据的输出

在模板文件中运行以下命令时,允许获取并打印货币汇率货币代码货币符号phtml

echo $block->getCurrentCurrencySymbol() . '<br />';
echo $block->getCurrentCurrencyCode() . '<br />';
echo $block->getBaseCurrencyCode() . '<br />';
echo $block->getDefaultCurrencyCode() . '<br />';
echo $block->getCurrentCurrencyRate() . '<br />';
 
print_r($block->getAvailableCurrencyCodes()) . '<br />';
print_r($block->getAllowedCurrencies()) . '<br />';

这是结束,我希望它对你有所帮助。如果您对文章或任何问题有任何疑问,请使用下面的评论部分!

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论