Skip to content

Commit 7a83091

Browse files
committed
Merge remote-tracking branch 'mainline/2.2-develop' into MAGETWO-72469
# Conflicts: # app/code/Magento/ConfigurableProductSales/composer.json # composer.json # composer.lock Merge remote-tracking branch 'mainline/2.2-develop' into MAGETWO-72469 # Conflicts: # composer.json # composer.lock
2 parents 5aac29c + e9893b3 commit 7a83091

File tree

11 files changed

+174
-173
lines changed

11 files changed

+174
-173
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
2929

3030
/**#@-*/
3131

32+
/**
33+
* Country column name for index value
34+
*/
35+
const COLUMN_COUNTRY_ID = 'country_id';
36+
37+
/**
38+
* Name of region id column
39+
*/
40+
const COLUMN_REGION_ID = 'region_id';
41+
3242
/**#@+
3343
* Particular columns that contains of customer default addresses
3444
*/
@@ -55,6 +65,13 @@ class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
5565
/**#@-*/
5666
protected $_permanentAttributes = [self::COLUMN_WEBSITE, self::COLUMN_EMAIL, self::COLUMN_ADDRESS_ID];
5767

68+
/**
69+
* Attributes with index (not label) value
70+
*
71+
* @var string[]
72+
*/
73+
protected $_indexValueAttributes = [self::COLUMN_COUNTRY_ID];
74+
5875
/**
5976
* Default addresses column names to appropriate customer attribute code
6077
*
@@ -149,7 +166,7 @@ public function __construct(
149166
$data['address_collection']
150167
) ? $data['address_collection'] : $addressColFactory->create();
151168

152-
$this->_initWebsites(true);
169+
$this->_initAttributeValues()->_initAttributeTypes()->_initWebsites(true);
153170
$this->setFileName($this->getEntityTypeCode());
154171
}
155172

@@ -243,6 +260,7 @@ public function exportItem($item)
243260
$row[self::COLUMN_ADDRESS_ID] = $item['entity_id'];
244261
$row[self::COLUMN_EMAIL] = $customer['email'];
245262
$row[self::COLUMN_WEBSITE] = $this->_websiteIdToCode[$customer['website_id']];
263+
$row[self::COLUMN_REGION_ID] = $item->getRegionId();
246264

247265
$this->getWriter()->writeRow($row);
248266
}

app/code/Magento/CustomerImportExport/Model/Export/Customer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function __construct(
112112
$data['customer_collection']
113113
) ? $data['customer_collection'] : $customerColFactory->create();
114114

115-
$this->_initAttributeValues()->_initStores()->_initWebsites(true);
115+
$this->_initAttributeValues()->_initAttributeTypes()->_initStores()->_initWebsites(true);
116116
}
117117

118118
/**

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

Lines changed: 27 additions & 0 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\CustomerImportExport\Model\ResourceModel\Import\Customer\Storage;
910
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1011

@@ -248,4 +249,30 @@ public function getCustomerStorage()
248249
{
249250
return $this->_customerStorage;
250251
}
252+
253+
/**
254+
* Returns id of option by value for select and multiselect attributes
255+
*
256+
* @param array $attributeParameters Parameters of an attribute
257+
* @param int|string $value A value of an attribute
258+
* @return int An option id of attribute
259+
*/
260+
protected function getSelectAttrIdByValue(array $attributeParameters, $value)
261+
{
262+
return isset($attributeParameters['options'][strtolower($value)])
263+
? $attributeParameters['options'][strtolower($value)]
264+
: 0;
265+
}
266+
267+
/**
268+
* Returns multiple value separator
269+
*
270+
* @return string
271+
*/
272+
protected function getMultipleValueSeparator()
273+
{
274+
return isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR])
275+
? $this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]
276+
: Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR;
277+
}
251278
}

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

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

8-
use Magento\ImportExport\Model\Import;
98
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
109

1110
/**
@@ -486,6 +485,7 @@ protected function _mergeEntityAttributes(array $newAttributes, array $attribute
486485
*/
487486
protected function _prepareDataForUpdate(array $rowData)
488487
{
488+
$multiSeparator = $this->getMultipleValueSeparator();
489489
$email = strtolower($rowData[self::COLUMN_EMAIL]);
490490
$customerId = $this->_getCustomerId($email, $rowData[self::COLUMN_WEBSITE]);
491491
// entity table data
@@ -523,20 +523,17 @@ protected function _prepareDataForUpdate(array $rowData)
523523
continue;
524524
}
525525
} elseif ($newAddress && !strlen($rowData[$attributeAlias])) {
526-
} elseif ('select' == $attributeParams['type']) {
527-
$value = $attributeParams['options'][strtolower($rowData[$attributeAlias])];
526+
} elseif (in_array($attributeParams['type'], ['select', 'boolean'])) {
527+
$value = $this->getSelectAttrIdByValue($attributeParams, mb_strtolower($rowData[$attributeAlias]));
528528
} elseif ('datetime' == $attributeParams['type']) {
529529
$value = (new \DateTime())->setTimestamp(strtotime($rowData[$attributeAlias]));
530530
$value = $value->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
531531
} elseif ('multiselect' == $attributeParams['type']) {
532-
$separator = isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]) ?
533-
$this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] :
534-
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR;
535-
$value = str_replace(
536-
$separator,
537-
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR,
538-
$rowData[$attributeAlias]
539-
);
532+
$ids = [];
533+
foreach (explode($multiSeparator, mb_strtolower($rowData[$attributeAlias])) as $subValue) {
534+
$ids[] = $this->getSelectAttrIdByValue($attributeParams, $subValue);
535+
}
536+
$value = implode(',', $ids);
540537
} else {
541538
$value = $rowData[$attributeAlias];
542539
}
@@ -722,6 +719,7 @@ protected function _isOptionalAddressEmpty(array $rowData)
722719
*/
723720
protected function _validateRowForUpdate(array $rowData, $rowNumber)
724721
{
722+
$multiSeparator = $this->getMultipleValueSeparator();
725723
if ($this->_checkUniqueKey($rowData, $rowNumber)) {
726724
$email = strtolower($rowData[self::COLUMN_EMAIL]);
727725
$website = $rowData[self::COLUMN_WEBSITE];
@@ -740,9 +738,6 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
740738
continue;
741739
}
742740
if (isset($rowData[$attributeCode]) && strlen($rowData[$attributeCode])) {
743-
$multiSeparator = isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]) ?
744-
$this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] :
745-
Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR;
746741
$this->isAttributeValid(
747742
$attributeCode,
748743
$attributeParams,

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

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

88
use Magento\Customer\Api\Data\CustomerInterface;
9+
use Magento\ImportExport\Model\Import;
910
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1011

1112
/**
@@ -338,6 +339,7 @@ protected function _getNextEntityId()
338339
*/
339340
protected function _prepareDataForUpdate(array $rowData)
340341
{
342+
$multiSeparator = $this->getMultipleValueSeparator();
341343
$entitiesToCreate = [];
342344
$entitiesToUpdate = [];
343345
$attributesToSave = [];
@@ -376,10 +378,14 @@ protected function _prepareDataForUpdate(array $rowData)
376378
// attribute values
377379
foreach (array_intersect_key($rowData, $this->_attributes) as $attributeCode => $value) {
378380
$attributeParameters = $this->_attributes[$attributeCode];
379-
if ('select' == $attributeParameters['type']) {
380-
$value = isset($attributeParameters['options'][strtolower($value)])
381-
? $attributeParameters['options'][strtolower($value)]
382-
: 0;
381+
if (in_array($attributeParameters['type'], ['select', 'boolean'])) {
382+
$value = $this->getSelectAttrIdByValue($attributeParameters, $value);
383+
} elseif ('multiselect' == $attributeParameters['type']) {
384+
$ids = [];
385+
foreach (explode($multiSeparator, mb_strtolower($value)) as $subValue) {
386+
$ids[] = $this->getSelectAttrIdByValue($attributeParameters, $subValue);
387+
}
388+
$value = implode(',', $ids);
383389
} elseif ('datetime' == $attributeParameters['type'] && !empty($value)) {
384390
$value = (new \DateTime())->setTimestamp(strtotime($value));
385391
$value = $value->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
@@ -535,7 +541,15 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
535541
continue;
536542
}
537543
if (isset($rowData[$attributeCode]) && strlen($rowData[$attributeCode])) {
538-
$this->isAttributeValid($attributeCode, $attributeParams, $rowData, $rowNumber);
544+
$this->isAttributeValid(
545+
$attributeCode,
546+
$attributeParams,
547+
$rowData,
548+
$rowNumber,
549+
isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR])
550+
? $this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]
551+
: Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR
552+
);
539553
} elseif ($attributeParams['is_required'] && !$this->_getCustomerId($email, $website)) {
540554
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, $attributeCode);
541555
}

app/code/Magento/Downloadable/Model/Quote/Item/CartItemProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function __construct(
6363
public function convertToBuyRequest(CartItemInterface $cartItem)
6464
{
6565
if ($cartItem->getProductOption()
66+
&& $cartItem->getProductOption()->getExtensionAttributes()
6667
&& $cartItem->getProductOption()->getExtensionAttributes()->getDownloadableOption()
6768
) {
6869
$downloadableLinks = $cartItem->getProductOption()->getExtensionAttributes()->getDownloadableOption()

app/code/Magento/Downloadable/Test/Unit/Model/Quote/Item/CartItemProcessorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ public function testConvertToBuyRequest()
111111
$this->assertEquals($buyRequestMock, $this->model->convertToBuyRequest($cartItemMock));
112112
}
113113

114+
public function testConvertToBuyRequestWithoutExtensionAttributes()
115+
{
116+
$cartItemMock = $this->createPartialMock(
117+
\Magento\Quote\Model\Quote\Item::class,
118+
['getProductOption', 'setProductOption', 'getOptionByCode', 'getQty']
119+
);
120+
$productOptionMock = $this->createMock(\Magento\Quote\Api\Data\ProductOptionInterface::class);
121+
122+
$cartItemMock->expects($this->any())->method('getProductOption')->willReturn($productOptionMock);
123+
$productOptionMock->expects($this->atLeastOnce())->method('getExtensionAttributes')->willReturn(null);
124+
125+
$this->assertNull($this->model->convertToBuyRequest($cartItemMock));
126+
}
127+
114128
public function testProcessProductOptions()
115129
{
116130
$downloadableLinks = [1, 2];

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

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
99
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
1010
use Magento\ImportExport\Model\Export;
11+
use Magento\ImportExport\Model\Import;
1112
use Magento\Store\Model\Store;
1213

1314
/**
@@ -27,6 +28,13 @@ abstract class AbstractEav extends \Magento\ImportExport\Model\Export\AbstractEn
2728
*/
2829
protected $_attributeValues = [];
2930

31+
/**
32+
* Attribute code to its types. Only attributes with options
33+
*
34+
* @var array
35+
*/
36+
protected $attributeTypes = [];
37+
3038
/**
3139
* Entity type id.
3240
*
@@ -88,6 +96,20 @@ protected function _initAttributeValues()
8896
return $this;
8997
}
9098

99+
/**
100+
* Initializes attribute types
101+
*
102+
* @return $this
103+
*/
104+
protected function _initAttributeTypes()
105+
{
106+
/** @var $attribute AbstractAttribute */
107+
foreach ($this->getAttributeCollection() as $attribute) {
108+
$this->attributeTypes[$attribute->getAttributeCode()] = $attribute->getFrontendInput();
109+
}
110+
return $this;
111+
}
112+
91113
/**
92114
* Apply filter to collection and add not skipped attributes to select
93115
*
@@ -246,19 +268,48 @@ protected function _addAttributeValuesToRow(\Magento\Framework\Model\AbstractMod
246268
foreach ($validAttributeCodes as $attributeCode) {
247269
$attributeValue = $item->getData($attributeCode);
248270

249-
if (isset(
250-
$this->_attributeValues[$attributeCode]
251-
) && isset(
252-
$this->_attributeValues[$attributeCode][$attributeValue]
253-
)
254-
) {
255-
$attributeValue = $this->_attributeValues[$attributeCode][$attributeValue];
256-
}
257-
if (null !== $attributeValue) {
258-
$row[$attributeCode] = $attributeValue;
271+
if ($this->isMultiselect($attributeCode)) {
272+
$values = [];
273+
$attributeValue = explode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $attributeValue);
274+
foreach ($attributeValue as $value) {
275+
$values[] = $this->getAttributeValueById($attributeCode, $value);
276+
}
277+
$row[$attributeCode] = implode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $values);
278+
} else {
279+
$row[$attributeCode] = $this->getAttributeValueById($attributeCode, $attributeValue);
259280
}
260281
}
261282

262283
return $row;
263284
}
285+
286+
/**
287+
* Checks that attribute is multiselect type by attribute code
288+
*
289+
* @param string $attributeCode An attribute code
290+
* @return bool Returns true if attribute is multiselect type
291+
*/
292+
private function isMultiselect($attributeCode)
293+
{
294+
return isset($this->attributeTypes[$attributeCode])
295+
&& $this->attributeTypes[$attributeCode] === 'multiselect';
296+
}
297+
298+
/**
299+
* Returns attribute value by id
300+
*
301+
* @param string $attributeCode An attribute code
302+
* @param int|string $valueId
303+
* @return mixed
304+
*/
305+
private function getAttributeValueById($attributeCode, $valueId)
306+
{
307+
if (isset($this->_attributeValues[$attributeCode])
308+
&& isset($this->_attributeValues[$attributeCode][$valueId])
309+
) {
310+
return $this->_attributeValues[$attributeCode][$valueId];
311+
}
312+
313+
return $valueId;
314+
}
264315
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,9 @@ public function isAttributeValid(
691691
break;
692692
case 'select':
693693
case 'multiselect':
694+
case 'boolean':
694695
$valid = true;
695-
foreach (explode($multiSeparator, strtolower($rowData[$attributeCode])) as $value) {
696+
foreach (explode($multiSeparator, mb_strtolower($rowData[$attributeCode])) as $value) {
696697
$valid = isset($attributeParams['options'][$value]);
697698
if (!$valid) {
698699
break;

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,7 @@ public function getAttributeOptions(
228228
foreach ($value as $innerOption) {
229229
// skip ' -- Please Select -- ' option
230230
if (strlen($innerOption['value'])) {
231-
if ($attribute->isStatic()) {
232-
$options[strtolower($innerOption[$index])] = $innerOption['value'];
233-
} else {
234-
// Non-static attributes flip keys an values
235-
$options[$innerOption['value']] = $innerOption[$index];
236-
}
231+
$options[mb_strtolower($innerOption[$index])] = $innerOption['value'];
237232
}
238233
}
239234
}

0 commit comments

Comments
 (0)