Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Currency Class Reference
Inheritance diagram for Currency:
Template AbstractBlock DataObject BlockInterface

Public Member Functions

 __construct (\Magento\Framework\View\Element\Template\Context $context, \Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Framework\Data\Helper\PostHelper $postDataHelper, \Magento\Framework\Locale\ResolverInterface $localeResolver, array $data=[])
 
 getCurrencyCount ()
 
 getCurrencies ()
 
 getSwitchUrl ()
 
 getSwitchCurrencyPostData ($code)
 
 getCurrentCurrencyCode ()
 
 getStoreCode ()
 
- Public Member Functions inherited from Template
 __construct (Template\Context $context, array $data=[])
 
 setTemplateContext ($templateContext)
 
 getTemplate ()
 
 setTemplate ($template)
 
 getTemplateFile ($template=null)
 
 getArea ()
 
 assign ($key, $value=null)
 
 fetchView ($fileName)
 
 getBaseUrl ()
 
 getObjectData (\Magento\Framework\DataObject $object, $key)
 
 getCacheKeyInfo ()
 
- Public Member Functions inherited from AbstractBlock
 __construct (\Magento\Framework\View\Element\Context $context, array $data=[])
 
 getJsLayout ()
 
 getRequest ()
 
 getParentBlock ()
 
 setLayout (\Magento\Framework\View\LayoutInterface $layout)
 
 getLayout ()
 
 setNameInLayout ($name)
 
 getChildNames ()
 
 setAttribute ($name, $value=null)
 
 setChild ($alias, $block)
 
 addChild ($alias, $block, $data=[])
 
 unsetChild ($alias)
 
 unsetCallChild ($alias, $callback, $result, $params)
 
 unsetChildren ()
 
 getChildBlock ($alias)
 
 getChildHtml ($alias='', $useCache=true)
 
 getChildChildHtml ($alias, $childChildAlias='', $useCache=true)
 
 getBlockHtml ($name)
 
 insert ($element, $siblingName=0, $after=true, $alias='')
 
 append ($element, $alias='')
 
 getGroupChildNames ($groupName)
 
 getChildData ($alias, $key='')
 
 getUiId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getJsId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getUrl ($route='', $params=[])
 
 getViewFileUrl ($fileId, array $params=[])
 
 formatDate ( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
 
 formatTime ( $time=null, $format=\IntlDateFormatter::SHORT, $showDate=false)
 
 getModuleName ()
 
 escapeHtml ($data, $allowedTags=null)
 
 escapeJs ($string)
 
 escapeHtmlAttr ($string, $escapeSingleQuote=true)
 
 escapeCss ($string)
 
 stripTags ($data, $allowableTags=null, $allowHtmlEntities=false)
 
 escapeUrl ($string)
 
 escapeXssInUrl ($data)
 
 escapeQuote ($data, $addSlashes=false)
 
 escapeJsQuote ($data, $quote='\'')
 
 getNameInLayout ()
 
 getCacheKeyInfo ()
 
 getCacheKey ()
 
 getVar ($name, $module=null)
 
 isScopePrivate ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 
- Public Member Functions inherited from BlockInterface
 toHtml ()
 

Protected Attributes

 $_currencyFactory
 
 $_postDataHelper
 
 $localeResolver
 
- Protected Attributes inherited from Template
 $_viewVars = []
 
 $_baseUrl
 
 $_jsUrl
 
 $_allowSymlinks
 
 $_filesystem
 
 $_template
 
 $templateEnginePool
 
 $_storeManager
 
 $_appState
 
 $directory
 
 $templateContext
 
 $pageConfig
 
 $resolver
 
 $validator
 
- Protected Attributes inherited from AbstractBlock
 $_design
 
 $_session
 
 $_sidResolver
 
 $_nameInLayout
 
 $_layout
 
 $jsLayout = []
 
 $_request
 
 $_urlBuilder
 
 $_eventManager
 
 $_frontController
 
 $_assetRepo
 
 $_viewConfig
 
 $_cacheState
 
 $_logger
 
 $_escaper
 
 $filterManager
 
 $_localeDate
 
 $inlineTranslation
 
 $_isScopePrivate = false
 
 $_scopeConfig
 
 $_cache
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Public Member Functions inherited from AbstractBlock
static extractModuleName ($className)
 
- Data Fields inherited from Template
const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink'
 
- Data Fields inherited from AbstractBlock
const CACHE_GROUP = \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER
 
const CACHE_KEY_PREFIX = 'BLOCK_'
 
- Protected Member Functions inherited from Template
 _construct ()
 
 _toHtml ()
 
 getRootDirectory ()
 
 getMediaDirectory ()
 
- Protected Member Functions inherited from AbstractBlock
 _construct ()
 
 _prepareLayout ()
 
 _beforeToHtml ()
 
 _afterToHtml ($html)
 
 _toHtml ()
 
 _getNotFoundUrl ($route='', $params=['_direct'=> 'core/index/notFound'])
 
 getCacheTags ()
 
 getCacheLifetime ()
 
 _loadCache ()
 
 _saveCache ($data)
 
 _getSidPlaceholder ($cacheKey=null)
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

@api

Since
100.0.2

Definition at line 18 of file Currency.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\View\Element\Template\Context  $context,
\Magento\Directory\Model\CurrencyFactory  $currencyFactory,
\Magento\Framework\Data\Helper\PostHelper  $postDataHelper,
\Magento\Framework\Locale\ResolverInterface  $localeResolver,
array  $data = [] 
)
Parameters
\Magento\Framework\View\Element\Template\Context$context
\Magento\Directory\Model\CurrencyFactory$currencyFactory
\Magento\Framework\Data\Helper\PostHelper$postDataHelper
\Magento\Framework\Locale\ResolverInterface$localeResolver
array$data

Definition at line 42 of file Currency.php.

48  {
49  $this->_currencyFactory = $currencyFactory;
50  $this->_postDataHelper = $postDataHelper;
51  parent::__construct($context, $data);
52  $this->localeResolver = $localeResolver;
53  }

Member Function Documentation

◆ getCurrencies()

getCurrencies ( )

Retrieve currencies array Return array: code => currency name Return empty array if only one currency

Returns
array

Definition at line 73 of file Currency.php.

74  {
75  $currencies = $this->getData('currencies');
76  if ($currencies === null) {
77  $currencies = [];
78  $codes = $this->_storeManager->getStore()->getAvailableCurrencyCodes(true);
79  if (is_array($codes) && count($codes) > 1) {
80  $rates = $this->_currencyFactory->create()->getCurrencyRates(
81  $this->_storeManager->getStore()->getBaseCurrency(),
82  $codes
83  );
84 
85  foreach ($codes as $code) {
86  if (isset($rates[$code])) {
87  $allCurrencies = (new CurrencyBundle())->get(
88  $this->localeResolver->getLocale()
89  )['Currencies'];
90  $currencies[$code] = $allCurrencies[$code][1] ?: $code;
91  }
92  }
93  }
94 
95  $this->setData('currencies', $currencies);
96  }
97  return $currencies;
98  }
getData($key='', $index=null)
Definition: DataObject.php:119
$rates
Definition: tax.phtml:35
setData($key, $value=null)
Definition: DataObject.php:72
$code
Definition: info.phtml:12

◆ getCurrencyCount()

getCurrencyCount ( )

Retrieve count of currencies Return 0 if only one currency

Returns
int

Definition at line 61 of file Currency.php.

62  {
63  return count($this->getCurrencies());
64  }

◆ getCurrentCurrencyCode()

getCurrentCurrencyCode ( )

Retrieve Current Currency code

Returns
string

Definition at line 126 of file Currency.php.

127  {
128  if ($this->_getData('current_currency_code') === null) {
129  // do not use $this->_storeManager->getStore()->getCurrentCurrencyCode() because of probability
130  // to get an invalid (without base rate) currency from code saved in session
131  $this->setData('current_currency_code', $this->_storeManager->getStore()->getCurrentCurrency()->getCode());
132  }
133  return $this->_getData('current_currency_code');
134  }
setData($key, $value=null)
Definition: DataObject.php:72

◆ getStoreCode()

getStoreCode ( )
Returns
string

Definition at line 139 of file Currency.php.

140  {
141  return $this->_storeManager->getStore()->getCode();
142  }

◆ getSwitchCurrencyPostData()

getSwitchCurrencyPostData (   $code)

Return POST data for currency to switch

Parameters
string$code
Returns
string

Definition at line 116 of file Currency.php.

117  {
118  return $this->_postDataHelper->getPostData($this->escapeUrl($this->getSwitchUrl()), ['currency' => $code]);
119  }
$code
Definition: info.phtml:12

◆ getSwitchUrl()

getSwitchUrl ( )

Retrieve Currency Switch URL

Returns
string

Definition at line 105 of file Currency.php.

106  {
107  return $this->getUrl('directory/currency/switch');
108  }

Field Documentation

◆ $_currencyFactory

$_currencyFactory
protected

Definition at line 23 of file Currency.php.

◆ $_postDataHelper

$_postDataHelper
protected

Definition at line 28 of file Currency.php.

◆ $localeResolver

$localeResolver
protected

Definition at line 33 of file Currency.php.


The documentation for this class was generated from the following file: