79 lines
2.4 KiB
PHP
79 lines
2.4 KiB
PHP
<?php
|
|
namespace Stemcell\ChineseDirectory\Helper;
|
|
|
|
use Magento\Directory\Model\AllowedCountries;
|
|
use Magento\Directory\Model\Region;
|
|
use Magento\Framework\App\Config\ScopeConfigInterface;
|
|
use Magento\Store\Model\ScopeInterface;
|
|
|
|
class Data extends \Magento\Directory\Helper\Data
|
|
{
|
|
protected const STORE_ID = 'store_id';
|
|
|
|
/**
|
|
* Get current scope from request
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function getCurrentScope(): array
|
|
{
|
|
$scope = [
|
|
'type' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
|
|
'value' => null,
|
|
];
|
|
$request = $this->_getRequest();
|
|
if ($request->getParam(ScopeInterface::SCOPE_WEBSITE)) {
|
|
$scope = [
|
|
'type' => ScopeInterface::SCOPE_WEBSITE,
|
|
'value' => $request->getParam(ScopeInterface::SCOPE_WEBSITE),
|
|
];
|
|
} elseif ($request->getParam(ScopeInterface::SCOPE_STORE)) {
|
|
$scope = [
|
|
'type' => ScopeInterface::SCOPE_STORE,
|
|
'value' => $request->getParam(ScopeInterface::SCOPE_STORE),
|
|
];
|
|
} elseif ($request->getParam(self::STORE_ID)) {
|
|
$scope = [
|
|
'type' => ScopeInterface::SCOPE_STORE,
|
|
'value' => $request->getParam(self::STORE_ID),
|
|
];
|
|
}
|
|
|
|
return $scope;
|
|
}
|
|
/**
|
|
* Retrieve regions data
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getRegionData()
|
|
{
|
|
$scope = $this->getCurrentScope();
|
|
$allowedCountries = $this->scopeConfig->getValue(
|
|
AllowedCountries::ALLOWED_COUNTRIES_PATH,
|
|
$scope['type'],
|
|
$scope['value']
|
|
);
|
|
$countryIds = explode(',', $allowedCountries);
|
|
$collection = $this->_regCollectionFactory->create();
|
|
$collection->addCountryFilter($countryIds)->load();
|
|
$regions = [
|
|
'config' => [
|
|
'show_all_regions' => $this->isShowNonRequiredState(),
|
|
'regions_required' => $this->getCountriesWithStatesRequired(),
|
|
],
|
|
];
|
|
foreach ($collection as $region) {
|
|
/** @var $region Region */
|
|
if (!$region->getRegionId()) {
|
|
continue;
|
|
}
|
|
$regions[$region->getCountryId()][$region->getCode()] = [
|
|
'code' => $region->getCode(),
|
|
'name' => (string)__($region->getName()),
|
|
];
|
|
}
|
|
return $regions;
|
|
}
|
|
}
|