/** * @param bool $isMultiselect * @return array */ public function toOptionArray($isMultiselect = false) { if (!$this->_options) { $countriesArray = $this->_countryCollectionFactory->create()->load()->toOptionArray(false); $this->_countries = []; foreach ($countriesArray as $a) { $this->_countries[$a['value']] = $a['label']; } $countryRegions = []; $regionsCollection = $this->_regionCollectionFactory->create()->load(); foreach ($regionsCollection as $region) { $countryRegions[$region->getCountryId()][$region->getId()] = $region->getDefaultName(); } uksort($countryRegions, [$this, 'sortRegionCountries']); $this->_options = []; foreach ($countryRegions as $countryId => $regions) { $regionOptions = []; foreach ($regions as $regionId => $regionName) { $regionOptions[] = ['label' => $regionName, 'value' => $regionId]; } $this->_options[] = ['label' => $this->_countries[$countryId], 'value' => $regionOptions]; } } $options = $this->_options; if (!$isMultiselect) { array_unshift($options, ['value' => '', 'label' => '']); } return $options; }
/** * @return RegionCollection */ public function getRegionCollection() { if (!$this->_regionCollection) { $this->_regionCollection = $this->_regionCollectionFactory->create()->addCountryFilter($this->getAddress()->getCountryId())->load(); } return $this->_regionCollection; }
public function testGetRegionJson() { $countries = array(new \Magento\Framework\Object(array('country_id' => 'Country1')), new \Magento\Framework\Object(array('country_id' => 'Country2'))); $countryIterator = new \ArrayIterator($countries); $this->_countryCollection->expects($this->atLeastOnce())->method('getIterator')->will($this->returnValue($countryIterator)); $regions = array(new \Magento\Framework\Object(array('country_id' => 'Country1', 'region_id' => 'r1', 'code' => 'r1-code', 'name' => 'r1-name')), new \Magento\Framework\Object(array('country_id' => 'Country1', 'region_id' => 'r2', 'code' => 'r2-code', 'name' => 'r2-name')), new \Magento\Framework\Object(array('country_id' => 'Country2', 'region_id' => 'r3', 'code' => 'r3-code', 'name' => 'r3-name'))); $regionIterator = new \ArrayIterator($regions); $this->_regionCollection->expects($this->once())->method('addCountryFilter')->with(array('Country1', 'Country2'))->will($this->returnSelf()); $this->_regionCollection->expects($this->once())->method('load'); $this->_regionCollection->expects($this->once())->method('getIterator')->will($this->returnValue($regionIterator)); $expectedDataToEncode = array('config' => array('show_all_regions' => false, 'regions_required' => array()), 'Country1' => array('r1' => array('code' => 'r1-code', 'name' => 'r1-name'), 'r2' => array('code' => 'r2-code', 'name' => 'r2-name')), 'Country2' => array('r3' => array('code' => 'r3-code', 'name' => 'r3-name'))); $this->_coreHelper->expects($this->once())->method('jsonEncode')->with(new \PHPUnit_Framework_Constraint_IsIdentical($expectedDataToEncode))->will($this->returnValue('encoded_json')); // Test $result = $this->_object->getRegionJson(); $this->assertEquals('encoded_json', $result); }
/** * Retrieve regions data json * * @return string */ public function getRegionJson() { \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]); if (!$this->_regionJson) { $cacheKey = 'DIRECTORY_REGIONS_JSON_STORE' . $this->_storeManager->getStore()->getId(); $json = $this->_configCacheType->load($cacheKey); if (empty($json)) { $countryIds = []; foreach ($this->getCountryCollection() as $country) { $countryIds[] = $country->getCountryId(); } $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 \Magento\Directory\Model\Region */ if (!$region->getRegionId()) { continue; } $regions[$region->getCountryId()][$region->getRegionId()] = ['code' => $region->getCode(), 'name' => (string) __($region->getName())]; } $json = $this->jsonHelper->jsonEncode($regions); if ($json === false) { $json = 'false'; } $this->_configCacheType->save($json, $cacheKey); } $this->_regionJson = $json; } \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__); return $this->_regionJson; }
/** * Return list of country's regions as array * * @param bool $noEmpty * @param string|array|null $country * @return array */ public function toOptionArray($noEmpty = false, $country = null) { /** @var $region \Magento\Directory\Model\Resource\Region\Collection */ $regionCollection = $this->_regionsFactory->create(); $options = $regionCollection->addCountryFilter($country)->toOptionArray(); if ($noEmpty) { unset($options[0]); } else { if ($options) { $options[0] = array('value' => '0', 'label' => '*'); } else { $options = array(array('value' => '0', 'label' => '*')); } } return $options; }
/** * Returns region code by name * * @param string $region * @param string $countryId * @return string */ public function getRegionIdByName($region, $countryId) { $collection = $this->regionCollectionFactory->create()->addRegionCodeOrNameFilter($region)->addCountryFilter($countryId); if ($collection->getSize()) { return $collection->getFirstItem()->getId(); } return ''; }
/** * Load directory regions * * @return array */ protected function loadDirectoryRegions() { $importRegions = []; /** @var $collection \Magento\Directory\Model\Resource\Region\Collection */ $collection = $this->regionCollectionFactory->create(); foreach ($collection->getData() as $row) { $importRegions[$row['country_id']][$row['code']] = (int) $row['region_id']; } return $importRegions; }
public function testGetRegionIdByName() { $regionCode = 'TX'; $countryId = 'US'; $regionId = 57; $regionCollectionMock = $this->getMockBuilder('\\Magento\\Directory\\Model\\Resource\\Region\\Collection')->disableOriginalConstructor()->getMock(); $this->regionCollectionFactoryMock->expects($this->once())->method('create')->willReturn($regionCollectionMock); $regionCollectionMock->expects($this->once())->method('addRegionCodeOrNameFilter')->with($regionCode)->willReturnSelf(); $regionCollectionMock->expects($this->once())->method('addCountryFilter')->with($countryId)->willReturnSelf(); $regionCollectionMock->expects($this->once())->method('getSize')->willReturn(1); $regionCollectionMock->expects($this->once())->method('getFirstItem')->willReturn(new \Magento\Framework\Object(['id' => $regionId])); $this->assertEquals($regionId, $this->block->getRegionIdByName($regionCode, $countryId)); }
/** * Retrieve regions data * * @return array */ public function getRegionData() { $countryIds = []; foreach ($this->getCountryCollection() as $country) { $countryIds[] = $country->getCountryId(); } $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 \Magento\Directory\Model\Region */ if (!$region->getRegionId()) { continue; } $regions[$region->getCountryId()][$region->getRegionId()] = ['code' => $region->getCode(), 'name' => (string) __($region->getName())]; } return $regions; }
/** * @return string */ public function getRegionsJs() { \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, array('group' => 'TEST', 'method' => __METHOD__)); $regionsJs = $this->getData('regions_js'); if (!$regionsJs) { $countryIds = array(); foreach ($this->getCountryCollection() as $country) { $countryIds[] = $country->getCountryId(); } $collection = $this->_regionCollectionFactory->create()->addCountryFilter($countryIds)->load(); $regions = array(); foreach ($collection as $region) { if (!$region->getRegionId()) { continue; } $regions[$region->getCountryId()][$region->getRegionId()] = array('code' => $region->getCode(), 'name' => $region->getName()); } $regionsJs = $this->_jsonEncoder->encode($regions); } \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__); return $regionsJs; }
/** * Load directory regions * * @return \Magento\OfflineShipping\Model\Resource\Carrier\Tablerate */ protected function _loadDirectoryRegions() { if (!is_null($this->_importRegions)) { return $this; } $this->_importRegions = array(); /** @var $collection \Magento\Directory\Model\Resource\Region\Collection */ $collection = $this->_regionCollectionFactory->create(); foreach ($collection->getData() as $row) { $this->_importRegions[$row['country_id']][$row['code']] = (int) $row['region_id']; } return $this; }
/** * @return \Magento\Directory\Model\Resource\Region\Collection */ protected function _createRegionsCollection() { return $this->_regionsFactory->create(); }
/** * @param \Magento\Framework\Stdlib\String $string * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\ImportExport\Model\ImportFactory $importFactory * @param \Magento\ImportExport\Model\Resource\Helper $resourceHelper * @param \Magento\Framework\App\Resource $resource * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\ImportExport\Model\Export\Factory $collectionFactory * @param \Magento\Eav\Model\Config $eavConfig * @param \Magento\CustomerImportExport\Model\Resource\Import\Customer\StorageFactory $storageFactory * @param \Magento\Customer\Model\AddressFactory $addressFactory * @param \Magento\Directory\Model\Resource\Region\CollectionFactory $regionColFactory * @param \Magento\Customer\Model\CustomerFactory $customerFactory * @param \Magento\Customer\Model\Resource\Address\CollectionFactory $addressColFactory * @param \Magento\Customer\Model\Resource\Address\Attribute\CollectionFactory $attributesFactory * @param \Magento\Framework\Stdlib\DateTime $dateTime * @param array $data * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct(\Magento\Framework\Stdlib\String $string, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\ImportExport\Model\ImportFactory $importFactory, \Magento\ImportExport\Model\Resource\Helper $resourceHelper, \Magento\Framework\App\Resource $resource, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\ImportExport\Model\Export\Factory $collectionFactory, \Magento\Eav\Model\Config $eavConfig, \Magento\CustomerImportExport\Model\Resource\Import\Customer\StorageFactory $storageFactory, \Magento\Customer\Model\AddressFactory $addressFactory, \Magento\Directory\Model\Resource\Region\CollectionFactory $regionColFactory, \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Customer\Model\Resource\Address\CollectionFactory $addressColFactory, \Magento\Customer\Model\Resource\Address\Attribute\CollectionFactory $attributesFactory, \Magento\Framework\Stdlib\DateTime $dateTime, array $data = []) { $this->_customerFactory = $customerFactory; $this->_addressFactory = $addressFactory; $this->_eavConfig = $eavConfig; $this->_resourceHelper = $resourceHelper; $this->dateTime = $dateTime; if (!isset($data['attribute_collection'])) { /** @var $attributeCollection \Magento\Customer\Model\Resource\Address\Attribute\Collection */ $attributeCollection = $attributesFactory->create(); $attributeCollection->addSystemHiddenFilter()->addExcludeHiddenFrontendFilter(); $data['attribute_collection'] = $attributeCollection; } parent::__construct($string, $scopeConfig, $importFactory, $resourceHelper, $resource, $storeManager, $collectionFactory, $eavConfig, $storageFactory, $data); $this->_addressCollection = isset($data['address_collection']) ? $data['address_collection'] : $addressColFactory->create(); $this->_entityTable = isset($data['entity_table']) ? $data['entity_table'] : $addressFactory->create()->getResource()->getEntityTable(); $this->_regionCollection = isset($data['region_collection']) ? $data['region_collection'] : $regionColFactory->create(); $this->addMessageTemplate(self::ERROR_ADDRESS_ID_IS_EMPTY, __('Customer address id column is not specified')); $this->addMessageTemplate(self::ERROR_ADDRESS_NOT_FOUND, __("Customer address for such customer doesn't exist")); $this->addMessageTemplate(self::ERROR_INVALID_REGION, __('Region is invalid')); $this->addMessageTemplate(self::ERROR_DUPLICATE_PK, __('Row with such email, website and address id combination was already found.')); $this->_initAttributes(); $this->_initAddresses()->_initCountryRegions(); }
/** * @return \Magento\Directory\Model\Resource\Region\Collection */ public function getRegionCollection() { $collection = $this->_regionCollectionFactory->create(); $collection->addCountryFilter($this->getId()); return $collection; }