_coreConfig = $coreConfig; $this->_configFactory = $configFactory; $this->_cacheTypeList = $cacheTypeList; $this->_storeManager = $storeManager; $this->localeResolver = $localeResolver; $this->_systemStore = $systemStore; $this->_eventManager = $eventManager; $this->_scopeConfig = $scopeConfig; $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class); } /** * Return currency symbol properties array based on config values * * @return array */ public function getCurrencySymbolsData() { if ($this->_symbolsData) { return $this->_symbolsData; } $this->_symbolsData = []; $currentSymbols = $this->_unserializeStoreConfig(self::XML_PATH_CUSTOM_CURRENCY_SYMBOL); foreach ($this->getAllowedCurrencies() as $code) { $currencies = (new CurrencyBundle())->get($this->localeResolver->getLocale())['Currencies']; $symbol = $currencies[$code][0] ?: $code; $name = $currencies[$code][1] ?: $code; $this->_symbolsData[$code] = ['parentSymbol' => $symbol, 'displayName' => $name]; if (isset($currentSymbols[$code]) && !empty($currentSymbols[$code])) { $this->_symbolsData[$code]['displaySymbol'] = $currentSymbols[$code]; } else { $this->_symbolsData[$code]['displaySymbol'] = $this->_symbolsData[$code]['parentSymbol']; } $this->_symbolsData[$code]['inherited'] = ($this->_symbolsData[$code]['parentSymbol'] == $this->_symbolsData[$code]['displaySymbol']); } return $this->_symbolsData; } /** * Save currency symbol to config * * @param array $symbols * @return $this */ public function setCurrencySymbolsData($symbols = []) { if (!$this->_storeManager->isSingleStoreMode()) { foreach ($this->getCurrencySymbolsData() as $code => $values) { if (isset($symbols[$code]) && ($symbols[$code] == $values['parentSymbol'] || empty($symbols[$code]))) { unset($symbols[$code]); } } } $value = []; if ($symbols) { $value['options']['fields']['customsymbol']['value'] = $this->serializer->serialize($symbols); } else { $value['options']['fields']['customsymbol']['inherit'] = 1; } $this->_configFactory->create() ->setSection(self::CONFIG_SECTION) ->setWebsite(null) ->setStore(null) ->setGroups($value) ->save(); $this->_eventManager->dispatch( 'admin_system_config_changed_section_currency_before_reinit', ['website' => $this->_websiteId, 'store' => $this->_storeId] ); // reinit configuration $this->_coreConfig->reinit(); $this->clearCache(); //Reset symbols cache since new data is added $this->_symbolsData = []; $this->_eventManager->dispatch( 'admin_system_config_changed_section_currency', ['website' => $this->_websiteId, 'store' => $this->_storeId] ); return $this; } /** * Return custom currency symbol by currency code * * @param string $code * @return string|false */ public function getCurrencySymbol($code) { $customSymbols = $this->_unserializeStoreConfig(self::XML_PATH_CUSTOM_CURRENCY_SYMBOL); if (array_key_exists($code, $customSymbols)) { return $customSymbols[$code]; } return false; } /** * Clear translate cache * * @return $this */ protected function clearCache() { // clear cache for frontend foreach ($this->_cacheTypes as $cacheType) { $this->_cacheTypeList->invalidate($cacheType); } return $this; } /** * Unserialize data from Store Config. * * @param string $configPath * @param int $storeId * @return array */ protected function _unserializeStoreConfig($configPath, $storeId = null) { $result = []; $configData = (string)$this->_scopeConfig->getValue( $configPath, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId ); if ($configData) { $result = $this->serializer->unserialize($configData); } return is_array($result) ? $result : []; } /** * Return allowed currencies * * @return array */ protected function getAllowedCurrencies() { $allowedCurrencies = explode( self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR, $this->_scopeConfig->getValue( self::XML_PATH_ALLOWED_CURRENCIES, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null ) ); $storeModel = $this->_systemStore; /** @var \Magento\Store\Model\Website $website */ foreach ($storeModel->getWebsiteCollection() as $website) { $websiteShow = false; /** @var \Magento\Store\Model\Group $group */ foreach ($storeModel->getGroupCollection() as $group) { if ($group->getWebsiteId() != $website->getId()) { continue; } /** @var \Magento\Store\Model\Store $store */ foreach ($storeModel->getStoreCollection() as $store) { if ($store->getGroupId() != $group->getId()) { continue; } if (!$websiteShow) { $websiteShow = true; $websiteSymbols = $website->getConfig(self::XML_PATH_ALLOWED_CURRENCIES); $allowedCurrencies = array_merge( $allowedCurrencies, explode(self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR, $websiteSymbols) ); } $storeSymbols = $this->_scopeConfig->getValue( self::XML_PATH_ALLOWED_CURRENCIES, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store ); $allowedCurrencies = array_merge( $allowedCurrencies, explode(self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR, $storeSymbols) ); } } } return array_unique($allowedCurrencies); } }