151 lines
6.1 KiB
PHP
151 lines
6.1 KiB
PHP
<?php
|
|
namespace Stemcell\Customer\Model;
|
|
|
|
use Stemcell\ChineseDirectory\Model\CollectionFactory;
|
|
use Magento\Customer\Api\AddressMetadataInterface;
|
|
use Magento\Customer\Api\Data\AddressInterface;
|
|
use Magento\Customer\Api\Data\AddressInterfaceFactory;
|
|
use Magento\Customer\Api\Data\RegionInterfaceFactory;
|
|
use Magento\Customer\Model\Address\CustomAttributeListInterface;
|
|
use Magento\Customer\Model\CustomerFactory;
|
|
use Magento\Directory\Helper\Data;
|
|
use Magento\Directory\Model\CountryFactory;
|
|
use Magento\Directory\Model\RegionFactory;
|
|
use Magento\Eav\Model\Config;
|
|
use Magento\Framework\Api\AttributeValueFactory;
|
|
use Magento\Framework\Api\DataObjectHelper;
|
|
use Magento\Framework\Api\ExtensionAttributesFactory;
|
|
use Magento\Framework\Data\Collection\AbstractDb;
|
|
use Magento\Framework\Indexer\IndexerRegistry;
|
|
use Magento\Framework\Model\Context;
|
|
use Magento\Framework\Model\ResourceModel\AbstractResource;
|
|
use Magento\Framework\Reflection\DataObjectProcessor;
|
|
use Magento\Framework\Registry;
|
|
|
|
class Address extends \Magento\Customer\Model\Address
|
|
{
|
|
protected CustomAttributeListInterface $attributeList;
|
|
|
|
protected CollectionFactory $collectionFactory;
|
|
|
|
public function __construct(
|
|
Context $context,
|
|
Registry $registry,
|
|
ExtensionAttributesFactory $extensionFactory,
|
|
AttributeValueFactory $customAttributeFactory,
|
|
Data $directoryData,
|
|
Config $eavConfig,
|
|
\Magento\Customer\Model\Address\Config $addressConfig,
|
|
RegionFactory $regionFactory,
|
|
CountryFactory $countryFactory,
|
|
AddressMetadataInterface $metadataService,
|
|
AddressInterfaceFactory $addressDataFactory,
|
|
RegionInterfaceFactory $regionDataFactory,
|
|
DataObjectHelper $dataObjectHelper,
|
|
CustomerFactory $customerFactory,
|
|
DataObjectProcessor $dataProcessor,
|
|
IndexerRegistry $indexerRegistry,
|
|
CollectionFactory $collectionFactory,
|
|
AbstractResource $resource = null,
|
|
AbstractDb $resourceCollection = null,
|
|
array $data = []
|
|
) {
|
|
$this->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;
|
|
}
|
|
}
|