Skip to content

Commit 70b8191

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-65667-Customer-Multiselect' into Okapis-PR
2 parents b868bcd + 4826364 commit 70b8191

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

app/code/Magento/CustomerImportExport/Model/Import/Address.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\CustomerImportExport\Model\Import;
77

8+
use Magento\ImportExport\Model\Import;
89
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
910

1011
/**
@@ -491,15 +492,13 @@ protected function _prepareDataForUpdate(array $rowData)
491492
{
492493
$email = strtolower($rowData[self::COLUMN_EMAIL]);
493494
$customerId = $this->_getCustomerId($email, $rowData[self::COLUMN_WEBSITE]);
494-
495495
// entity table data
496496
$entityRowNew = [];
497497
$entityRowUpdate = [];
498498
// attribute values
499499
$attributes = [];
500500
// customer default addresses
501501
$defaults = [];
502-
503502
$newAddress = true;
504503
// get address id
505504
if (isset(
@@ -519,7 +518,6 @@ protected function _prepareDataForUpdate(array $rowData)
519518
'parent_id' => $customerId,
520519
'updated_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
521520
];
522-
523521
foreach ($this->_attributes as $attributeAlias => $attributeParams) {
524522
if (array_key_exists($attributeAlias, $rowData)) {
525523
if (!strlen($rowData[$attributeAlias])) {
@@ -534,6 +532,15 @@ protected function _prepareDataForUpdate(array $rowData)
534532
} elseif ('datetime' == $attributeParams['type']) {
535533
$value = (new \DateTime())->setTimestamp(strtotime($rowData[$attributeAlias]));
536534
$value = $value->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
535+
} elseif ('multiselect' == $attributeParams['type']) {
536+
$separator = isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]) ?
537+
$this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] :
538+
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR;
539+
$value = str_replace(
540+
$separator,
541+
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR,
542+
$rowData[$attributeAlias]
543+
);
537544
} else {
538545
$value = $rowData[$attributeAlias];
539546
}
@@ -544,15 +551,13 @@ protected function _prepareDataForUpdate(array $rowData)
544551
}
545552
}
546553
}
547-
548554
foreach (self::getDefaultAddressAttributeMapping() as $columnName => $attributeCode) {
549555
if (!empty($rowData[$columnName])) {
550556
/** @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
551557
$table = $this->_getCustomerEntity()->getResource()->getTable('customer_entity');
552558
$defaults[$table][$customerId][$attributeCode] = $addressId;
553559
}
554560
}
555-
556561
// let's try to find region ID
557562
$entityRow['region_id'] = null;
558563
if (!empty($rowData[self::COLUMN_REGION])) {
@@ -565,7 +570,6 @@ protected function _prepareDataForUpdate(array $rowData)
565570
$entityRow['region_id'] = $regionId;
566571
}
567572
}
568-
569573
if ($newAddress) {
570574
$entityRowNew = $entityRow;
571575
$entityRowNew['created_at'] =
@@ -740,7 +744,16 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
740744
continue;
741745
}
742746
if (isset($rowData[$attributeCode]) && strlen($rowData[$attributeCode])) {
743-
$this->isAttributeValid($attributeCode, $attributeParams, $rowData, $rowNumber);
747+
$multiSeparator = isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]) ?
748+
$this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] :
749+
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR;
750+
$this->isAttributeValid(
751+
$attributeCode,
752+
$attributeParams,
753+
$rowData,
754+
$rowNumber,
755+
$multiSeparator
756+
);
744757
} elseif ($attributeParams['is_required'] && (!isset(
745758
$this->_addresses[$customerId]
746759
) || !in_array(

app/code/Magento/ImportExport/Model/Import/AbstractEntity.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Framework\App\ObjectManager;
99
use Magento\Framework\App\ResourceConnection;
1010
use Magento\Framework\Serialize\Serializer\Json;
11+
use Magento\ImportExport\Model\Import;
1112
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
1213
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1314

@@ -74,8 +75,8 @@ abstract class AbstractEntity
7475
self::ERROR_CODE_COLUMNS_NUMBER => "Number of columns does not correspond to the number of rows in the header",
7576
self::ERROR_EXCEEDED_MAX_LENGTH => 'Attribute %s exceeded max length',
7677
self::ERROR_INVALID_ATTRIBUTE_TYPE => 'Value for \'%s\' attribute contains incorrect value',
77-
self::ERROR_INVALID_ATTRIBUTE_OPTION =>
78-
"Value for %s attribute contains incorrect value, see acceptable values on settings specified for Admin",
78+
self::ERROR_INVALID_ATTRIBUTE_OPTION => "Value for %s attribute contains incorrect value"
79+
. ", see acceptable values on settings specified for Admin",
7980
];
8081

8182
/**#@-*/
@@ -665,11 +666,17 @@ public function getMasterAttributeCode()
665666
* @param array $attributeParams Attribute params
666667
* @param array $rowData Row data
667668
* @param int $rowNumber
669+
* @param string $multiSeparator
668670
* @return bool
669671
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
670672
*/
671-
public function isAttributeValid($attributeCode, array $attributeParams, array $rowData, $rowNumber)
672-
{
673+
public function isAttributeValid(
674+
$attributeCode,
675+
array $attributeParams,
676+
array $rowData,
677+
$rowNumber,
678+
$multiSeparator = Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR
679+
) {
673680
$message = '';
674681
switch ($attributeParams['type']) {
675682
case 'varchar':
@@ -684,7 +691,13 @@ public function isAttributeValid($attributeCode, array $attributeParams, array $
684691
break;
685692
case 'select':
686693
case 'multiselect':
687-
$valid = isset($attributeParams['options'][strtolower($rowData[$attributeCode])]);
694+
$valid = true;
695+
foreach (explode($multiSeparator, strtolower($rowData[$attributeCode])) as $value) {
696+
$valid = isset($attributeParams['options'][$value]);
697+
if (!$valid) {
698+
break;
699+
}
700+
}
688701
$message = self::ERROR_INVALID_ATTRIBUTE_OPTION;
689702
break;
690703
case 'int':

app/code/Magento/ImportExport/Model/Import/Entity/AbstractEav.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,14 @@ public function getAttributeOptions(
225225
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
226226
$value = is_array($option['value']) ? $option['value'] : [$option];
227227
foreach ($value as $innerOption) {
228+
// skip ' -- Please Select -- ' option
228229
if (strlen($innerOption['value'])) {
229-
// skip ' -- Please Select -- ' option
230-
$options[strtolower($innerOption[$index])] = $innerOption['value'];
230+
if ($attribute->isStatic()) {
231+
$options[strtolower($innerOption[$index])] = $innerOption['value'];
232+
} else {
233+
// Non-static attributes flip keys an values
234+
$options[$innerOption['value']] = $innerOption[$index];
235+
}
231236
}
232237
}
233238
}

0 commit comments

Comments
 (0)