本文整理汇总了PHP中Magento\Directory\Model\CurrencyFactory类的典型用法代码示例。如果您正苦于以下问题:PHP CurrencyFactory类的具体用法?PHP CurrencyFactory怎么用?PHP CurrencyFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CurrencyFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* @param \Magento\Backend\Block\Context $context
* @param \Magento\Framework\StoreManagerInterface $storeManager
* @param \Magento\Directory\Model\Currency\DefaultLocator $currencyLocator
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
* @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
* @param array $data
*/
public function __construct(\Magento\Backend\Block\Context $context, \Magento\Framework\StoreManagerInterface $storeManager, \Magento\Directory\Model\Currency\DefaultLocator $currencyLocator, \Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Framework\Locale\CurrencyInterface $localeCurrency, array $data = array())
{
parent::__construct($context, $data);
$this->_storeManager = $storeManager;
$this->_currencyLocator = $currencyLocator;
$this->_localeCurrency = $localeCurrency;
$defaultBaseCurrencyCode = $this->_scopeConfig->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, 'default');
$this->_defaultBaseCurrency = $currencyFactory->create()->load($defaultBaseCurrencyCode);
}
示例2: _prepareLayout
/**
* Prepare layout
*
* @return \Magento\Framework\View\Element\AbstractBlock
*/
protected function _prepareLayout()
{
$newRates = $this->_backendSession->getRates();
$this->_backendSession->unsetData('rates');
$currencyModel = $this->_dirCurrencyFactory->create();
$currencies = $currencyModel->getConfigAllowCurrencies();
$defaultCurrencies = $currencyModel->getConfigBaseCurrencies();
$oldCurrencies = $this->_prepareRates($currencyModel->getCurrencyRates($defaultCurrencies, $currencies));
foreach ($currencies as $currency) {
foreach ($oldCurrencies as $key => $value) {
if (!array_key_exists($currency, $oldCurrencies[$key])) {
$oldCurrencies[$key][$currency] = '';
}
}
}
foreach ($oldCurrencies as $key => $value) {
ksort($oldCurrencies[$key]);
}
sort($currencies);
$this->setAllowedCurrencies($currencies)->setDefaultCurrencies($defaultCurrencies)->setOldRates($oldCurrencies)->setNewRates($this->_prepareRates($newRates));
return parent::_prepareLayout();
}
示例3: _beforeToHtml
/**
* Execute before toHtml() code.
*
* @return $this
*/
public function _beforeToHtml()
{
$this->_currency = $this->_currencyFactory->create()->load($this->_scopeConfig->getValue(Currency::XML_PATH_CURRENCY_BASE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
$this->_collection = $this->_collectionFactory->create()->setCustomerIdFilter((int) $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID))->setOrderStateFilter(Order::STATE_CANCELED, true)->load();
$this->_groupedCollection = [];
foreach ($this->_collection as $sale) {
if ($sale->getStoreId() !== null) {
$store = $this->_storeManager->getStore($sale->getStoreId());
$websiteId = $store->getWebsiteId();
$groupId = $store->getGroupId();
$storeId = $store->getId();
$sale->setWebsiteId($store->getWebsiteId());
$sale->setWebsiteName($store->getWebsite()->getName());
$sale->setGroupId($store->getGroupId());
$sale->setGroupName($store->getGroup()->getName());
} else {
$websiteId = 0;
$groupId = 0;
$storeId = 0;
$sale->setStoreName(__('Deleted Stores'));
}
$this->_groupedCollection[$websiteId][$groupId][$storeId] = $sale;
$this->_websiteCounts[$websiteId] = isset($this->_websiteCounts[$websiteId]) ? $this->_websiteCounts[$websiteId] + 1 : 1;
}
return parent::_beforeToHtml();
}
示例4: _saveRates
/**
* Saving currency rates
*
* @param array $rates
* @return \Magento\Directory\Model\Currency\Import\AbstractImport
*/
protected function _saveRates($rates)
{
foreach ($rates as $currencyCode => $currencyRates) {
$this->_currencyFactory->create()->setId($currencyCode)->setRates($currencyRates)->save();
}
return $this;
}
示例5: testFetchRates
public function testFetchRates()
{
$currencyFromList = ['USD'];
$currencyToList = ['EUR', 'UAH'];
$responseBody = '{"query":{"count":7,"created":"2016-04-05T16:46:55Z","lang":"en-US","results":{"rate":' . '[{"id":"USDEUR","Name":"USD/EUR","Rate":"0.9022","Date":"4/5/2016"}]}}}';
$expectedCurrencyRateList = ['USD' => ['EUR' => 0.9022, 'UAH' => null]];
$message = "We can't retrieve a rate from http://query.yahooapis.com/v1/public/yql?format=json" . "&q=select+*+from+yahoo.finance.xchange+where+pair+in+%28%22USDEUR%22%2C%22USDUAH%22)" . "&env=store://datatables.org/alltableswithkeys for UAH.";
/** @var \Magento\Directory\Model\Currency|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
$currencyMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency')->disableOriginalConstructor()->setMethods([])->getMock();
/** @var \Magento\Framework\HTTP\ZendClient|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
$httpClientMock = $this->getMockBuilder('Magento\\Framework\\HTTP\\ZendClient')->disableOriginalConstructor()->setMethods([])->getMock();
/** @var \Zend_Http_Response|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
$httpResponseMock = $this->getMockBuilder('Zend_Http_Response')->disableOriginalConstructor()->setMethods([])->getMock();
$this->currencyFactoryMock->expects($this->any())->method('create')->willReturn($currencyMock);
$currencyMock->expects($this->once())->method('getConfigBaseCurrencies')->willReturn($currencyFromList);
$currencyMock->expects($this->once())->method('getConfigAllowCurrencies')->willReturn($currencyToList);
$this->httpClientFactoryMock->expects($this->any())->method('create')->willReturn($httpClientMock);
$httpClientMock->expects($this->atLeastOnce())->method('setUri')->willReturnSelf();
$httpClientMock->expects($this->atLeastOnce())->method('setConfig')->willReturnSelf();
$httpClientMock->expects($this->atLeastOnce())->method('request')->willReturn($httpResponseMock);
$httpResponseMock->expects($this->any())->method('getBody')->willReturn($responseBody);
$this->assertEquals($expectedCurrencyRateList, $this->model->fetchRates());
$messages = $this->model->getMessages();
$this->assertNotEmpty($messages);
$this->assertTrue(is_array($messages));
$this->assertEquals($message, (string) $messages[0]);
}
示例6: testFetchRates
public function testFetchRates()
{
$currencyFromList = ['USD'];
$currencyToList = ['EUR', 'UAH'];
$responseBody = '{"base":"USD","date":"2015-10-07","rates":{"EUR":0.9022}}';
$expectedCurrencyRateList = ['USD' => ['EUR' => 0.9022, 'UAH' => null]];
$message = "We can't retrieve a rate from http://api.fixer.io/latest?base=USD&symbols=EUR,UAH for UAH.";
/** @var \Magento\Directory\Model\Currency|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
$currencyMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency')->disableOriginalConstructor()->setMethods([])->getMock();
/** @var \Magento\Framework\HTTP\ZendClient|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
$httpClientMock = $this->getMockBuilder('Magento\\Framework\\HTTP\\ZendClient')->disableOriginalConstructor()->setMethods([])->getMock();
/** @var \Zend_Http_Response|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
$httpResponseMock = $this->getMockBuilder('Zend_Http_Response')->disableOriginalConstructor()->setMethods([])->getMock();
$this->currencyFactoryMock->expects($this->any())->method('create')->willReturn($currencyMock);
$currencyMock->expects($this->once())->method('getConfigBaseCurrencies')->willReturn($currencyFromList);
$currencyMock->expects($this->once())->method('getConfigAllowCurrencies')->willReturn($currencyToList);
$this->httpClientFactoryMock->expects($this->any())->method('create')->willReturn($httpClientMock);
$httpClientMock->expects($this->atLeastOnce())->method('setUri')->willReturnSelf();
$httpClientMock->expects($this->atLeastOnce())->method('setConfig')->willReturnSelf();
$httpClientMock->expects($this->atLeastOnce())->method('request')->willReturn($httpResponseMock);
$httpResponseMock->expects($this->any())->method('getBody')->willReturn($responseBody);
$this->assertEquals($expectedCurrencyRateList, $this->model->fetchRates());
$messages = $this->model->getMessages();
$this->assertNotEmpty($messages);
$this->assertTrue(is_array($messages));
$this->assertEquals($message, (string) $messages[0]);
}
示例7: afterSave
/**
* Check base currency is available in installed currencies
*
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function afterSave()
{
$value = $this->getValue();
if (!in_array($value, $this->_getInstalledCurrencies())) {
throw new \Magento\Framework\Exception\LocalizedException(__('Sorry, we haven\'t installed the base currency you selected.'));
}
$this->currencyFactory->create()->saveRates([$value => [$value => 1]]);
return $this;
}
示例8: getCurrency
/**
* {@inheritdoc}
*/
public function getCurrency($scope = null, $currency = null)
{
if ($currency instanceof Currency) {
$currentCurrency = $currency;
} elseif (is_string($currency)) {
$currency = $this->currencyFactory->create()->load($currency);
$baseCurrency = $this->getStore($scope)->getBaseCurrency();
$currentCurrency = $baseCurrency->getRate($currency) ? $currency : $baseCurrency;
} else {
$currentCurrency = $this->getStore($scope)->getCurrentCurrency();
}
return $currentCurrency;
}
示例9: testConvertWithCurrencyString
public function testConvertWithCurrencyString()
{
$amount = 5.6;
$currency = 'ru';
$convertedAmount = 9.300000000000001;
$currentCurrency = $this->getCurrentCurrencyMock();
$currentCurrency->expects($this->once())->method('load')->with($currency)->will($this->returnSelf());
$this->currencyFactory->expects($this->once())->method('create')->will($this->returnValue($currentCurrency));
$baseCurrency = $this->getBaseCurrencyMock($amount, $convertedAmount, $currentCurrency);
$baseCurrency->expects($this->once())->method('getRate')->with($currentCurrency)->will($this->returnValue(1.2));
$store = $this->getStoreMock($baseCurrency);
$this->assertEquals($convertedAmount, $this->priceCurrency->convert($amount, $store, $currency));
}
示例10: aroundGetPriceFormat
/**
* Modify precision for JPY
*
* @param \Magento\Framework\Locale\Format $subject Currency Format Obj
* @param \Closure $proceed Closure
* @param null|string $localeCode Locale Code
* @param null|string $currencyCode Currency Code
*
* @return mixed
*/
public function aroundGetPriceFormat(Format $subject, \Closure $proceed, $localeCode = null, $currencyCode = null)
{
if ($currencyCode) {
$currency = $this->_currencyFactory->create()->load($currencyCode);
} else {
$currency = $this->_scopeResolver->getScope()->getCurrentCurrency();
}
$result = $proceed($localeCode, $currencyCode);
if ($currency->getCode() == 'JPY') {
$result['precision'] = '0';
$result['requiredPrecision'] = '0';
}
return $result;
}
示例11: getAvailableCurrencies
/**
* Retrieve avilable currency codes
*
* @return string[]
*/
public function getAvailableCurrencies()
{
$dirtyCodes = $this->getStore()->getAvailableCurrencyCodes();
$codes = [];
if (is_array($dirtyCodes) && count($dirtyCodes)) {
$rates = $this->_currencyFactory->create()->getCurrencyRates($this->_storeManager->getStore()->getBaseCurrency(), $dirtyCodes);
foreach ($dirtyCodes as $code) {
if (isset($rates[$code]) || $code == $this->_storeManager->getStore()->getBaseCurrencyCode()) {
$codes[] = $code;
}
}
}
return $codes;
}
示例12: testScheduledUpdateCurrencyRates
public function testScheduledUpdateCurrencyRates()
{
$this->scopeConfig->expects($this->at(0))->method('getValue')->with(Observer::IMPORT_ENABLE, ScopeInterface::SCOPE_STORE)->will($this->returnValue(1));
$this->scopeConfig->expects($this->at(1))->method('getValue')->with(Observer::CRON_STRING_PATH, ScopeInterface::SCOPE_STORE)->will($this->returnValue('cron-path'));
$this->scopeConfig->expects($this->at(2))->method('getValue')->with(Observer::IMPORT_SERVICE, ScopeInterface::SCOPE_STORE)->will($this->returnValue('import-service'));
$importInterfaceMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency\\Import\\Webservicex')->disableOriginalConstructor()->setMethods(['fetchRates', 'getMessages'])->getMock();
$importInterfaceMock->expects($this->once())->method('fetchRates')->will($this->returnValue([]));
$importInterfaceMock->expects($this->once())->method('getMessages')->will($this->returnValue([]));
$this->importFactory->expects($this->once())->method('create')->with('import-service')->will($this->returnValue($importInterfaceMock));
$currencyMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency')->disableOriginalConstructor()->setMethods(['saveRates', '__wakeup', '__sleep'])->getMock();
$currencyMock->expects($this->once())->method('saveRates')->will($this->returnValue(null));
$this->currencyFactory->expects($this->once())->method('create')->will($this->returnValue($currencyMock));
$this->observer->scheduledUpdateCurrencyRates(null);
}
示例13: scheduledUpdateCurrencyRates
/**
* @param mixed $schedule
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function scheduledUpdateCurrencyRates($schedule)
{
$importWarnings = [];
if (!$this->_scopeConfig->getValue(self::IMPORT_ENABLE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE) || !$this->_scopeConfig->getValue(self::CRON_STRING_PATH, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
return;
}
$errors = [];
$rates = [];
$service = $this->_scopeConfig->getValue(self::IMPORT_SERVICE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
if ($service) {
try {
$importModel = $this->_importFactory->create($service);
$rates = $importModel->fetchRates();
$errors = $importModel->getMessages();
} catch (\Exception $e) {
$importWarnings[] = __('FATAL ERROR:') . ' ' . __('We can\'t initialize the import model.');
}
} else {
$importWarnings[] = __('FATAL ERROR:') . ' ' . __('Please specify the correct Import Service.');
}
if (sizeof($errors) > 0) {
foreach ($errors as $error) {
$importWarnings[] = __('WARNING:') . ' ' . $error;
}
}
if (sizeof($importWarnings) == 0) {
$this->_currencyFactory->create()->saveRates($rates);
} else {
$this->inlineTranslation->suspend();
$this->_transportBuilder->setTemplateIdentifier($this->_scopeConfig->getValue(self::XML_PATH_ERROR_TEMPLATE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE))->setTemplateOptions(['area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID])->setTemplateVars(['warnings' => join("\n", $importWarnings)])->setFrom($this->_scopeConfig->getValue(self::XML_PATH_ERROR_IDENTITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE))->addTo($this->_scopeConfig->getValue(self::XML_PATH_ERROR_RECIPIENT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
$transport = $this->_transportBuilder->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
}
}
示例14: _prepareWebsiteDateTable
/**
* Prepare website current dates table
*
* @return \Magento\Catalog\Model\Indexer\Product\Price\AbstractAction
*/
protected function _prepareWebsiteDateTable()
{
$baseCurrency = $this->_config->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE);
$select = $this->_connection->select()->from(['cw' => $this->_defaultIndexerResource->getTable('store_website')], ['website_id'])->join(['csg' => $this->_defaultIndexerResource->getTable('store_group')], 'cw.default_group_id = csg.group_id', ['store_id' => 'default_store_id'])->where('cw.website_id != 0');
$data = [];
foreach ($this->_connection->fetchAll($select) as $item) {
/** @var $website \Magento\Store\Model\Website */
$website = $this->_storeManager->getWebsite($item['website_id']);
if ($website->getBaseCurrencyCode() != $baseCurrency) {
$rate = $this->_currencyFactory->create()->load($baseCurrency)->getRate($website->getBaseCurrencyCode());
if (!$rate) {
$rate = 1;
}
} else {
$rate = 1;
}
/** @var $store \Magento\Store\Model\Store */
$store = $this->_storeManager->getStore($item['store_id']);
if ($store) {
$timestamp = $this->_localeDate->scopeTimeStamp($store);
$data[] = ['website_id' => $website->getId(), 'website_date' => $this->_dateTime->formatDate($timestamp, false), 'rate' => $rate];
}
}
$table = $this->_defaultIndexerResource->getTable('catalog_product_index_website');
$this->_emptyTable($table);
if ($data) {
foreach ($data as $row) {
$this->_connection->insertOnDuplicate($table, $row, array_keys($row));
}
}
return $this;
}
示例15: afterSave
/**
* After Save Attribute manipulation
*
* @param \Magento\Catalog\Model\Product $object
* @return $this
*/
public function afterSave($object)
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
/**
* Orig value is only for existing objects
*/
$oridData = $object->getOrigData();
$origValueExist = $oridData && array_key_exists($this->getAttribute()->getAttributeCode(), $oridData);
if ($object->getStoreId() != 0 || !$value || $origValueExist) {
return $this;
}
if ($this->getAttribute()->getIsGlobal() == \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_WEBSITE) {
$baseCurrency = $this->_config->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, 'default');
$storeIds = $object->getStoreIds();
if (is_array($storeIds)) {
foreach ($storeIds as $storeId) {
$storeCurrency = $this->_storeManager->getStore($storeId)->getBaseCurrencyCode();
if ($storeCurrency == $baseCurrency) {
continue;
}
$rate = $this->_currencyFactory->create()->load($baseCurrency)->getRate($storeCurrency);
if (!$rate) {
$rate = 1;
}
$newValue = $value * $rate;
$object->addAttributeUpdate($this->getAttribute()->getAttributeCode(), $newValue, $storeId);
}
}
}
return $this;
}