Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
8 
10 
18 {
22  const OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH = 'general/country/optional_zip_countries';
23 
24  /*
25  * Path to config value, which lists countries, for which state is required.
26  */
27  const XML_PATH_STATES_REQUIRED = 'general/region/state_required';
28 
29  /*
30  * Path to config value, which detects whether or not display the state for the country, if it is not required
31  */
32  const XML_PATH_DISPLAY_ALL_STATES = 'general/region/display_all';
33 
37  const XML_PATH_DEFAULT_COUNTRY = 'general/country/default';
38  const XML_PATH_DEFAULT_LOCALE = 'general/locale/code';
39  const XML_PATH_DEFAULT_TIMEZONE = 'general/locale/timezone';
46  const XML_PATH_TOP_COUNTRIES = 'general/country/destinations';
47 
51  const XML_PATH_WEIGHT_UNIT = 'general/locale/weight_unit';
52 
59 
65  protected $_regionCollection;
66 
72  protected $_regionJson;
73 
79  protected $_currencyCache = [];
80 
86  protected $_optZipCountries = null;
87 
91  protected $_configCacheType;
92 
97 
101  protected $jsonHelper;
102 
106  protected $_storeManager;
107 
111  protected $_currencyFactory;
112 
122  public function __construct(
123  \Magento\Framework\App\Helper\Context $context,
124  \Magento\Framework\App\Cache\Type\Config $configCacheType,
125  \Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection,
126  \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regCollectionFactory,
127  \Magento\Framework\Json\Helper\Data $jsonHelper,
128  \Magento\Store\Model\StoreManagerInterface $storeManager,
129  \Magento\Directory\Model\CurrencyFactory $currencyFactory
130  ) {
131  parent::__construct($context);
132  $this->_configCacheType = $configCacheType;
133  $this->_countryCollection = $countryCollection;
134  $this->_regCollectionFactory = $regCollectionFactory;
135  $this->jsonHelper = $jsonHelper;
136  $this->_storeManager = $storeManager;
137  $this->_currencyFactory = $currencyFactory;
138  }
139 
145  public function getRegionCollection()
146  {
147  if (!$this->_regionCollection) {
148  $this->_regionCollection = $this->_regCollectionFactory->create();
149  $this->_regionCollection->addCountryFilter($this->getAddress()->getCountryId())->load();
150  }
152  }
153 
160  public function getCountryCollection($store = null)
161  {
162  if (!$this->_countryCollection->isLoaded()) {
163  $this->_countryCollection->loadByStore($store);
164  }
166  }
167 
173  public function getRegionJson()
174  {
175  \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
176  if (!$this->_regionJson) {
177  $cacheKey = 'DIRECTORY_REGIONS_JSON_STORE' . $this->_storeManager->getStore()->getId();
178  $json = $this->_configCacheType->load($cacheKey);
179  if (empty($json)) {
180  $regions = $this->getRegionData();
181  $json = $this->jsonHelper->jsonEncode($regions);
182  if ($json === false) {
183  $json = 'false';
184  }
185  $this->_configCacheType->save($json, $cacheKey);
186  }
187  $this->_regionJson = $json;
188  }
189 
190  \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
191  return $this->_regionJson;
192  }
193 
203  public function currencyConvert($amount, $from, $to = null)
204  {
205  if (empty($this->_currencyCache[$from])) {
206  $this->_currencyCache[$from] = $this->_currencyFactory->create()->load($from);
207  }
208  if ($to === null) {
209  $to = $this->_storeManager->getStore()->getCurrentCurrencyCode();
210  }
211  $converted = $this->_currencyCache[$from]->convert($amount, $to);
212  return $converted;
213  }
214 
221  public function getCountriesWithOptionalZip($asJson = false)
222  {
223  if (null === $this->_optZipCountries) {
224  $value = trim(
225  $this->scopeConfig->getValue(
226  self::OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH,
228  )
229  );
230  $this->_optZipCountries = preg_split('/\,/', $value, 0, PREG_SPLIT_NO_EMPTY);
231  }
232  if ($asJson) {
233  return $this->jsonHelper->jsonEncode($this->_optZipCountries);
234  }
236  }
237 
244  public function isZipCodeOptional($countryCode)
245  {
247  return in_array($countryCode, $this->_optZipCountries);
248  }
249 
256  public function getCountriesWithStatesRequired($asJson = false)
257  {
258  $value = trim(
259  $this->scopeConfig->getValue(
260  self::XML_PATH_STATES_REQUIRED,
262  )
263  );
264  $countryList = preg_split('/\,/', $value, 0, PREG_SPLIT_NO_EMPTY);
265  if ($asJson) {
266  return $this->jsonHelper->jsonEncode($countryList);
267  }
268  return $countryList;
269  }
270 
276  public function isShowNonRequiredState()
277  {
278  return (bool)$this->scopeConfig->getValue(
279  self::XML_PATH_DISPLAY_ALL_STATES,
281  );
282  }
283 
290  public function isRegionRequired($countryId)
291  {
292  $countyList = $this->getCountriesWithStatesRequired();
293  if (!is_array($countyList)) {
294  return false;
295  }
296  return in_array($countryId, $countyList);
297  }
298 
304  public function getBaseCurrencyCode()
305  {
306  return $this->scopeConfig->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, 'default');
307  }
308 
315  public function getDefaultCountry($store = null)
316  {
317  return $this->scopeConfig->getValue(
318  self::XML_PATH_DEFAULT_COUNTRY,
320  $store
321  );
322  }
323 
329  public function getRegionData()
330  {
331  $countryIds = [];
332  foreach ($this->getCountryCollection() as $country) {
333  $countryIds[] = $country->getCountryId();
334  }
335  $collection = $this->_regCollectionFactory->create();
336  $collection->addCountryFilter($countryIds)->load();
337  $regions = [
338  'config' => [
339  'show_all_regions' => $this->isShowNonRequiredState(),
340  'regions_required' => $this->getCountriesWithStatesRequired(),
341  ],
342  ];
343  foreach ($collection as $region) {
345  if (!$region->getRegionId()) {
346  continue;
347  }
348  $regions[$region->getCountryId()][$region->getRegionId()] = [
349  'code' => $region->getCode(),
350  'name' => (string)__($region->getName()),
351  ];
352  }
353  return $regions;
354  }
355 
361  public function getTopCountryCodes()
362  {
363  $configValue = (string)$this->scopeConfig->getValue(
364  self::XML_PATH_TOP_COUNTRIES,
366  );
367  return !empty($configValue) ? explode(',', $configValue) : [];
368  }
369 
375  public function getWeightUnit()
376  {
377  return $this->scopeConfig->getValue(self::XML_PATH_WEIGHT_UNIT, ScopeInterface::SCOPE_STORE);
378  }
379 }
isZipCodeOptional($countryCode)
Definition: Data.php:244
getCountriesWithOptionalZip($asJson=false)
Definition: Data.php:221
const OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH
Definition: Data.php:22
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\App\Cache\Type\Config $configCacheType, \Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection, \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regCollectionFactory, \Magento\Framework\Json\Helper\Data $jsonHelper, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Directory\Model\CurrencyFactory $currencyFactory)
Definition: Data.php:122
isRegionRequired($countryId)
Definition: Data.php:290
$storeManager
__()
Definition: __.php:13
$amount
Definition: order.php:14
getDefaultCountry($store=null)
Definition: Data.php:315
currencyConvert($amount, $from, $to=null)
Definition: Data.php:203
$value
Definition: gender.phtml:16
getCountriesWithStatesRequired($asJson=false)
Definition: Data.php:256
getCountryCollection($store=null)
Definition: Data.php:160