collectionFactory = $collectionFactory; parent::__construct( $context, $registry, $extensionFactory, $customAttributeFactory, $directoryData, $eavConfig, $addressConfig, $regionFactory, $countryFactory, $metadataService, $addressDataFactory, $regionDataFactory, $dataObjectHelper, $customerFactory, $dataProcessor, $indexerRegistry, $resource, $resourceCollection, $data ); } /** * @param String $type * @param int $administrativeId * @return \Magento\Framework\DataObject */ public function getAdministrativeName(String $type, $administrativeId) { if ($administrativeId===null){ $this->_logger->error('Argument ($administrativeId) is null, Argument ($type):'.$type); return new \Magento\Framework\DataObject(); } if (!in_array(strtolower($type), ['city', 'district', 'town', 'region'])) { throw new \InvalidArgumentException(__('Invalid parameter type')); } $collection = $this->collectionFactory->create(strtolower($type)) ->addFieldToFilter('code', $administrativeId) ->addFieldToSelect('*'); if ($type == 'region') { $collection->addFieldToFilter('country_id', 'CN'); } return $collection->getFirstItem(); } public function updateData(AddressInterface $address) { // Set all attributes $attributes = $this->dataProcessor ->buildOutputDataArray($address, AddressInterface::class); foreach ($attributes as $attributeCode => $attributeData) { if (AddressInterface::REGION === $attributeCode) { $this->setRegion($address->getRegion()->getRegion()); $this->setRegionCode($address->getRegion()->getRegionCode()); $this->setRegionId($address->getRegion()->getRegionId()); } else { $this->setDataUsingMethod($attributeCode, $attributeData); } } if (!$this->getCity()) { $cityCode = $address->getCustomAttribute('city_code')? $address->getCustomAttribute('city_code')->getValue():null; $city = $this->getAdministrativeName('city', $cityCode); $this->setData('city', $city->getData('name')); } $this->setData('postcode', '0000000'); $districtCode = $address->getCustomAttribute('district_code')? $address->getCustomAttribute('district_code')->getValue():null; $district = $this->getAdministrativeName('district', $districtCode); $this->setCustomAttribute('district', $district->getData('name')); $districtCode = $address->getCustomAttribute('town_code')? $address->getCustomAttribute('town_code')->getValue():null; $district = $this->getAdministrativeName('town', $districtCode); $this->setCustomAttribute('town', $district->getData('name')); $districtCode = $address->getCustomAttribute('city_code')? $address->getCustomAttribute('city_code')->getValue():null; $this->setCustomAttribute('city_code', $districtCode); $districtCode = $address->getCustomAttribute('district_code')? $address->getCustomAttribute('district_code')->getValue():null; $this->setCustomAttribute('district_code', $districtCode); $districtCode = $address->getCustomAttribute('town_code')? $address->getCustomAttribute('town_code')->getValue():null; $this->setCustomAttribute('town_code', $districtCode); // Need to explicitly set this due to discrepancy in the keys between model and data object $this->setIsDefaultBilling($address->isDefaultBilling()); $this->setIsDefaultShipping($address->isDefaultShipping()); $customAttributes = $address->getCustomAttributes(); if ($customAttributes !== null) { foreach ($customAttributes as $attribute) { $this->setData($attribute->getAttributeCode(), $attribute->getValue()); } } return $this; } }