79 self::COLUMN_DEFAULT_BILLING =>
'default_billing',
80 self::COLUMN_DEFAULT_SHIPPING =>
'default_shipping',
216 "region_id",
"vat_is_valid",
"vat_request_date",
"vat_request_id",
"vat_request_success" 227 private $countryWithWebsites;
234 private $optionsByWebsite = [];
239 private $addressStorage;
283 CountryWithWebsitesSource $countryWithWebsites =
null,
284 AddressStorage $addressStorage =
null 289 $this->_resourceHelper = $resourceHelper;
292 $this->countryWithWebsites = $countryWithWebsites ?:
295 if (!isset(
$data[
'attribute_collection'])) {
315 $this->_entityTable = isset(
316 $data[
'entity_table']
318 $this->_regionCollection = isset(
319 $data[
'region_collection']
320 ) ?
$data[
'region_collection'] : $regionColFactory->create();
322 $this->
addMessageTemplate(self::ERROR_ADDRESS_ID_IS_EMPTY,
__(
'Customer address id column is not specified'));
324 self::ERROR_ADDRESS_NOT_FOUND,
325 __(
'We can\'t find that customer address.')
329 self::ERROR_DUPLICATE_PK,
330 __(
'We found another row with this email, website and address ID combination.')
332 $this->addressStorage = $addressStorage
335 $this->_initAttributes();
336 $this->_initCountryRegions();
344 $standardOptions = parent::getAttributeOptions(
$attribute, $indexAttributes);
346 if (
$attribute->getAttributeCode() ===
'country_id') {
349 $options = $this->countryWithWebsites->getAllOptions();
355 if (array_key_exists(
'website_ids',
$option)) {
357 if (!array_key_exists(
$websiteId, $websiteOptions)) {
366 $this->optionsByWebsite[
$code] = $websiteOptions;
369 return $standardOptions;
385 $attributeOptions = $this->optionsByWebsite[
$attributeData[
'code']];
386 if (array_key_exists(
$websiteId, $attributeOptions)) {
401 if (!$this->_customerEntity) {
402 $this->_customerEntity = $this->_customerFactory->create();
412 protected function _getNextEntityId()
414 if (!$this->_nextEntityId) {
416 $addressResource = $this->_addressFactory->create()->getResource();
417 $addressTable = $addressResource->getEntityTable();
418 $this->_nextEntityId = $this->_resourceHelper->getNextAutoincrement($addressTable);
420 return $this->_nextEntityId++;
428 protected function _initCountryRegions()
431 foreach ($this->_regionCollection as $region) {
432 $countryNormalized = strtolower($region->getCountryId());
433 $regionCode = strtolower($region->getCode());
434 $regionName = strtolower($region->getDefaultName());
435 $this->_countryRegions[$countryNormalized][$regionCode] = $region->getId();
436 $this->_countryRegions[$countryNormalized][$regionName] = $region->getId();
437 $this->_regions[$region->getId()] = $region->getDefaultName();
454 $customersPresent = [];
455 foreach ($rows as $rowData) {
456 $email = $rowData[static::COLUMN_EMAIL] ??
null;
457 $websiteId = isset($rowData[static::COLUMN_WEBSITE])
458 ? $this->
getWebsiteId($rowData[static::COLUMN_WEBSITE]) :
false;
460 $customersPresent[] = [
471 $customerData[
'email'],
479 $this->addressStorage->prepareAddresses($ids);
489 return parent::validateData();
503 while ($bunch = $this->_dataSourceModel->getNextBunch()) {
504 $rows = array_merge($rows, $bunch);
507 unset($bunch, $rows);
508 $this->_dataSourceModel->getIterator()->rewind();
511 while ($bunch = $this->_dataSourceModel->getNextBunch()) {
519 foreach ($bunch as $rowNumber => $rowData) {
530 $addUpdateResult = $this->_prepareDataForUpdate($rowData);
531 if ($addUpdateResult[
'entity_row_new']) {
532 $newRows[] = $addUpdateResult[
'entity_row_new'];
534 if ($addUpdateResult[
'entity_row_update']) {
535 $updateRows[] = $addUpdateResult[
'entity_row_update'];
547 ->_saveCustomerDefaults($defaults);
563 foreach ($newAttributes as
$tableName => $tableData) {
564 foreach ($tableData as $entityId => $entityData) {
565 foreach ($entityData as $attributeId => $attributeValue) {
581 protected function _prepareDataForUpdate(array $rowData):array
584 $email = strtolower($rowData[self::COLUMN_EMAIL]);
588 $entityRowUpdate = [];
595 if ($rowData[self::COLUMN_ADDRESS_ID]
596 && $this->addressStorage->doesExist(
597 $rowData[self::COLUMN_ADDRESS_ID],
604 $addressId = $this->_getNextEntityId();
607 'entity_id' => $addressId,
609 'updated_at' => (new \DateTime())->format(\
Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
613 foreach ($this->_attributes as $attributeAlias => $attributeParams) {
614 if (array_key_exists($attributeAlias, $rowData)) {
615 $attributeParams = $this->adjustAttributeDataForWebsite($attributeParams,
$websiteId);
617 if (!strlen($rowData[$attributeAlias])) {
623 }
elseif ($newAddress && !strlen($rowData[$attributeAlias])) {
624 }
elseif (in_array($attributeParams[
'type'], [
'select',
'boolean'])) {
626 }
elseif (
'datetime' == $attributeParams[
'type']) {
627 $value = (new \DateTime())->setTimestamp(strtotime($rowData[$attributeAlias]));
629 }
elseif (
'multiselect' == $attributeParams[
'type']) {
631 foreach (explode($multiSeparator, mb_strtolower($rowData[$attributeAlias])) as $subValue) {
634 $value = implode(
',', $ids);
636 $value = $rowData[$attributeAlias];
638 if ($attributeParams[
'is_static']) {
639 $entityRow[$attributeAlias] =
$value;
641 $attributes[$attributeParams[
'table']][$addressId][$attributeParams[
'id']]=
$value;
645 foreach (self::getDefaultAddressAttributeMapping() as $columnName =>
$attributeCode) {
646 if (!empty($rowData[$columnName])) {
653 $entityRow[
'region_id'] =
null;
654 if (!empty($rowData[self::COLUMN_REGION])) {
655 $countryNormalized = strtolower($rowData[self::COLUMN_COUNTRY_ID]);
656 $regionNormalized = strtolower($rowData[self::COLUMN_REGION]);
658 if (isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
659 $regionId = $this->_countryRegions[$countryNormalized][$regionNormalized];
661 $entityRow[
'region_id'] = $regionId;
665 $entityRowNew = $entityRow;
666 $entityRowNew[
'created_at'] =
667 (new \DateTime())->format(\
Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
669 $entityRowUpdate = $entityRow;
673 'entity_row_new' => $entityRowNew,
674 'entity_row_update' => $entityRowUpdate,
676 'defaults' => $defaults
690 $this->_connection->insertMultiple($this->_entityTable, $addRows);
694 foreach ($updateRows as
$row) {
695 $fields = array_diff(array_keys(
$row), [
'entity_id',
'parent_id',
'created_at']);
696 $this->_connection->insertOnDuplicate($this->_entityTable,
$row,
$fields);
715 'entity_id' => $addressId,
716 'attribute_id' => $attributeId,
721 $this->_connection->insertOnDuplicate(
$tableName, $tableData, [
'value']);
741 $this->_connection->insertOnDuplicate(
$tableName,
$data, array_keys($defaultsData));
756 $this->_connection->delete($this->_entityTable, [
'entity_id IN (?)' => $entityRowIds]);
769 return 'customer_address';
791 if (empty($this->_customerAttributes)) {
800 foreach ($rowData as $key =>
$value) {
801 if (!in_array($key, $this->_customerAttributes) && !empty(
$value)) {
822 $email = strtolower($rowData[self::COLUMN_EMAIL]);
827 if ($customerId ===
false) {
828 $this->
addRowError(self::ERROR_CUSTOMER_NOT_FOUND, $rowNumber);
831 $this->
addRowError(self::ERROR_DUPLICATE_PK, $rowNumber);
834 foreach ($this->_attributes as
$attributeCode => $attributeParams) {
836 $attributeParams = $this->adjustAttributeDataForWebsite($attributeParams,
$websiteId);
849 }
elseif ($attributeParams[
'is_required']
850 && !$this->addressStorage->doesExist(
859 if (isset($rowData[self::COLUMN_POSTCODE])
860 && isset($rowData[self::COLUMN_COUNTRY_ID])
861 && !$this->postcodeValidator->isValid(
862 $rowData[self::COLUMN_COUNTRY_ID],
863 $rowData[self::COLUMN_POSTCODE]
866 $this->
addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, self::COLUMN_POSTCODE);
869 if (isset($rowData[self::COLUMN_COUNTRY_ID]) && isset($rowData[self::COLUMN_REGION])) {
870 $countryRegions = isset(
871 $this->_countryRegions[strtolower($rowData[self::COLUMN_COUNTRY_ID])]
872 ) ? $this->_countryRegions[strtolower(
873 $rowData[self::COLUMN_COUNTRY_ID]
876 if (!empty($rowData[self::COLUMN_REGION]) && !empty($countryRegions) && !isset(
877 $countryRegions[strtolower($rowData[self::COLUMN_REGION])]
880 $this->
addRowError(self::ERROR_INVALID_REGION, $rowNumber, self::COLUMN_REGION);
898 $email = strtolower($rowData[self::COLUMN_EMAIL]);
903 if ($customerId ===
false) {
904 $this->
addRowError(self::ERROR_CUSTOMER_NOT_FOUND, $rowNumber);
906 if (!strlen($addressId)) {
907 $this->
addRowError(self::ERROR_ADDRESS_ID_IS_EMPTY, $rowNumber);
908 }
elseif (!$this->addressStorage->doesExist(
912 $this->
addRowError(self::ERROR_ADDRESS_NOT_FOUND, $rowNumber);
927 if ($this->addressStorage->doesExist(
931 if (!isset($this->_importedRowPks[
$customerId][$addressId])) {
932 $this->_importedRowPks[
$customerId][$addressId] =
true;
950 $this->_customerAttributes = $customerAttributes;
_validateRowForDelete(array $rowData, $rowNumber)
isAttributeValid( $attributeCode, array $attributeParams, array $rowData, $rowNumber, $multiSeparator=Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR)
elseif(isset( $params[ 'redirect_parent']))
_saveAddressAttributes(array $attributesData)
const COLUMN_DEFAULT_SHIPPING
getMultipleValueSeparator()
const BEHAVIOR_ADD_UPDATE
static getDefaultAddressAttributeMapping()
addRowError( $errorCode, $errorRowNum, $colName=null, $errorMessage=null, $errorLevel=ProcessingError::ERROR_LEVEL_CRITICAL, $errorDescription=null)
_isOptionalAddressEmpty(array $rowData)
const ERROR_ADDRESS_ID_IS_EMPTY
_saveAddressEntities(array $addRows, array $updateRows)
static $_defaultAddressAttributeMapping
_checkUniqueKey(array $rowData, $rowNumber)
prepareCustomerData($rows)
_validateRowForUpdate(array $rowData, $rowNumber)
getWebsiteId($websiteCode)
getBehavior(array $rowData=null)
_deleteAddressEntities(array $entityRowIds)
const ATTRIBUTE_COLLECTION_NAME
const ERROR_ADDRESS_NOT_FOUND
const ERROR_INVALID_REGION
getAttributeOptions(AbstractAttribute $attribute, array $indexAttributes=[])
getSelectAttrIdByValue(array $attributeParameters, $value)
_mergeEntityAttributes(array $newAttributes, array $attributes)
addMessageTemplate($errorCode, $message)
_getCustomerId($email, $websiteCode)
updateItemsCounterStats(array $created=[], array $updated=[], array $deleted=[])
setCustomerAttributes($customerAttributes)
__construct(\Magento\Framework\Stdlib\StringUtils $string, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\ImportExport\Model\ImportFactory $importFactory, \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper, \Magento\Framework\App\ResourceConnection $resource, ProcessingErrorAggregatorInterface $errorAggregator, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\ImportExport\Model\Export\Factory $collectionFactory, \Magento\Eav\Model\Config $eavConfig, \Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory $storageFactory, array $data=[])
_saveCustomerDefaults(array $defaults)
_checkRowDuplicate($customerId, $addressId)
const COLUMN_DEFAULT_BILLING
validateRow(array $rowData, $rowNumber)