Skip to content

Commit a9a467a

Browse files
committed
MAGETWO-90149: Product Import does not allow store-specific Custom Option labels
1 parent 40514e4 commit a9a467a

File tree

7 files changed

+51
-41
lines changed

7 files changed

+51
-41
lines changed

app/code/Magento/BundleImportExport/Model/Export/RowCustomizer.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
use Magento\CatalogImportExport\Model\Import\Product as ImportProductModel;
1111
use Magento\Bundle\Model\ResourceModel\Selection\Collection as SelectionCollection;
1212
use Magento\ImportExport\Model\Import as ImportModel;
13-
use \Magento\Catalog\Model\Product\Type\AbstractType;
14-
use \Magento\Framework\App\ObjectManager;
15-
use \Magento\Store\Model\StoreManagerInterface;
13+
use Magento\Catalog\Model\Product\Type\AbstractType;
14+
use Magento\Store\Model\StoreManagerInterface;
1615

1716
/**
1817
* Class RowCustomizer
@@ -128,7 +127,6 @@ class RowCustomizer implements RowCustomizerInterface
128127

129128
/**
130129
* @param StoreManagerInterface $storeManager
131-
* @throws \RuntimeException
132130
*/
133131
public function __construct(StoreManagerInterface $storeManager)
134132
{
@@ -235,9 +233,9 @@ protected function populateBundleData($collection)
235233
* @param \Magento\Catalog\Model\Product $product
236234
* @return string
237235
*/
238-
protected function getFormattedBundleOptionValues($product)
236+
protected function getFormattedBundleOptionValues(\Magento\Catalog\Model\Product $product): string
239237
{
240-
$optionCollections = $this->getProductOptionCollections($product);
238+
$optionCollections = $this->getProductOptionCollection($product);
241239
$bundleData = '';
242240
$optionTitles = $this->getBundleOptionTitles($product);
243241
foreach ($optionCollections->getItems() as $option) {
@@ -298,8 +296,10 @@ function ($value, $key) {
298296
* @param string[] $optionTitles
299297
* @return string
300298
*/
301-
protected function getFormattedOptionValues($option, $optionTitles = [])
302-
{
299+
protected function getFormattedOptionValues(
300+
\Magento\Bundle\Model\Option $option,
301+
array $optionTitles = []
302+
): string {
303303
$names = implode(ImportModel::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, array_map(
304304
function ($title, $storeName) {
305305
return $storeName . ImportProductModel::PAIR_NAME_VALUE_SEPARATOR . $title;
@@ -433,7 +433,7 @@ private function parseAdditionalAttributes($additionalAttributes)
433433
*/
434434
private function getBundleOptionTitles(\Magento\Catalog\Model\Product $product): array
435435
{
436-
$optionCollections = $this->getProductOptionCollections($product);
436+
$optionCollections = $this->getProductOptionCollection($product);
437437
$optionsTitles = [];
438438
/** @var \Magento\Bundle\Model\Option $option */
439439
foreach ($optionCollections->getItems() as $option) {
@@ -442,12 +442,13 @@ private function getBundleOptionTitles(\Magento\Catalog\Model\Product $product):
442442
$storeIds = $product->getStoreIds();
443443
if (count($storeIds) > 1) {
444444
foreach ($storeIds as $storeId) {
445-
$optionCollections = $this->getProductOptionCollections($product, $storeId);
445+
$optionCollections = $this->getProductOptionCollection($product, (int)$storeId);
446446
/** @var \Magento\Bundle\Model\Option $option */
447447
foreach ($optionCollections->getItems() as $option) {
448448
$optionTitle = $option->getTitle();
449449
if ($optionsTitles[$option->getId()]['name'] != $optionTitle) {
450-
$optionsTitles[$option->getId()]['name_' . $this->getStoreCodeById($storeId)] = $optionTitle;
450+
$optionsTitles[$option->getId()]['name_' . $this->getStoreCodeById((int)$storeId)] =
451+
$optionTitle;
451452
}
452453
}
453454
}
@@ -464,9 +465,9 @@ private function getBundleOptionTitles(\Magento\Catalog\Model\Product $product):
464465
* @param int $storeId
465466
* @return \Magento\Bundle\Model\ResourceModel\Option\Collection
466467
*/
467-
private function getProductOptionCollections(
468+
private function getProductOptionCollection(
468469
\Magento\Catalog\Model\Product $product,
469-
$storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID
470+
int $storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID
470471
): \Magento\Bundle\Model\ResourceModel\Option\Collection {
471472
$productSku = $product->getSku();
472473
if (!isset($this->optionCollections[$productSku][$storeId])) {
@@ -487,7 +488,7 @@ private function getProductOptionCollections(
487488
* @param int $storeId
488489
* @return string
489490
*/
490-
private function getStoreCodeById($storeId): string
491+
private function getStoreCodeById(int $storeId): string
491492
{
492493
if (!isset($this->storeIdToCode[$storeId])) {
493494
$this->storeIdToCode[$storeId] = $this->storeManager->getStore($storeId)->getCode();

app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
*/
99
namespace Magento\BundleImportExport\Model\Import\Product\Type;
1010

11-
use \Magento\Framework\App\ObjectManager;
12-
use \Magento\Bundle\Model\Product\Price as BundlePrice;
13-
use \Magento\Catalog\Model\Product\Type\AbstractType;
14-
use \Magento\CatalogImportExport\Model\Import\Product;
15-
use \Magento\Store\Model\StoreManagerInterface;
11+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
12+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Bundle\Model\Product\Price as BundlePrice;
15+
use Magento\Catalog\Model\Product\Type\AbstractType;
16+
use Magento\CatalogImportExport\Model\Import\Product;
17+
use Magento\Framework\App\ResourceConnection;
18+
use Magento\Framework\EntityManager\MetadataPool;
19+
use Magento\Store\Model\StoreManagerInterface;
1620

1721
/**
1822
* Class Bundle
@@ -148,20 +152,20 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
148152
private $storeCodeToId = [];
149153

150154
/**
151-
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac
152-
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttrColFac
153-
* @param \Magento\Framework\App\ResourceConnection $resource
155+
* @param AttributeSetCollectionFactory $attrSetColFac
156+
* @param AttributeCollectionFactory $prodAttrColFac
157+
* @param ResourceConnection $resource
154158
* @param array $params
155-
* @param \Magento\Framework\EntityManager\MetadataPool|null $metadataPool
159+
* @param MetadataPool|null $metadataPool
156160
* @param Bundle\RelationsDataSaver|null $relationsDataSaver
157-
* @param StoreManagerInterface $storeManager
161+
* @param StoreManagerInterface|null $storeManager
158162
*/
159163
public function __construct(
160-
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac,
161-
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttrColFac,
162-
\Magento\Framework\App\ResourceConnection $resource,
164+
AttributeSetCollectionFactory $attrSetColFac,
165+
AttributeCollectionFactory $prodAttrColFac,
166+
ResourceConnection $resource,
163167
array $params,
164-
\Magento\Framework\EntityManager\MetadataPool $metadataPool = null,
168+
MetadataPool $metadataPool = null,
165169
Bundle\RelationsDataSaver $relationsDataSaver = null,
166170
StoreManagerInterface $storeManager = null
167171
) {
@@ -279,10 +283,10 @@ protected function populateOptionTemplate($option, $entityId, $index = null)
279283
* @param int $storeId
280284
* @return array
281285
*/
282-
protected function populateOptionValueTemplate($option, $optionId, $storeId = 0)
286+
protected function populateOptionValueTemplate(array $option, int $optionId, int $storeId = 0): array
283287
{
284288
$optionValues = [];
285-
if (isset($option['name']) && isset($option['parent_id']) && $optionId) {
289+
if (isset($option['name'], $option['parent_id']) && $optionId) {
286290
$pattern = '/^name[_]?(.*)/';
287291
$keys = array_keys($option);
288292
$optionNames = preg_grep($pattern, $keys);
@@ -298,6 +302,7 @@ protected function populateOptionValueTemplate($option, $optionId, $storeId = 0)
298302
];
299303
}
300304
}
305+
301306
return $optionValues;
302307
}
303308

@@ -597,7 +602,7 @@ protected function insertOptions()
597602
* @param array $optionIds
598603
* @return array
599604
*/
600-
protected function populateInsertOptionValues($optionIds)
605+
protected function populateInsertOptionValues(array $optionIds): array
601606
{
602607
$optionValues = [];
603608
foreach ($this->_cachedOptions as $entityId => $options) {
@@ -616,6 +621,7 @@ protected function populateInsertOptionValues($optionIds)
616621
}
617622
}
618623
}
624+
619625
return $optionValues;
620626
}
621627

@@ -752,6 +758,7 @@ private function getStoreIdByCode(string $storeCode): int
752758
$this->storeCodeToId[$store->getCode()] = $store->getId();
753759
}
754760
}
761+
755762
return $this->storeCodeToId[$storeCode];
756763
}
757764
}

app/code/Magento/CatalogImportExport/Model/Import/Product/Option.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,10 @@ protected function _findExistingOptionId(array $newOptionData, array $newOptionT
760760
ksort($newOptionTitles);
761761
$existingOptions = $this->_oldCustomOptions[$productId];
762762
foreach ($existingOptions as $optionId => $optionData) {
763-
if ($optionData['type'] == $newOptionData['type'] && $optionData['titles'][0] == $newOptionTitles[0]) {
763+
if (
764+
$optionData['type'] == $newOptionData['type']
765+
&& $optionData['titles'][Store::DEFAULT_STORE_ID] == $newOptionTitles[Store::DEFAULT_STORE_ID]
766+
) {
764767
return $optionId;
765768
}
766769
}
@@ -1158,7 +1161,7 @@ private function processOptionRow($name, $optionRow)
11581161
* @param array $optionRow
11591162
* @return array
11601163
*/
1161-
private function addPriceData($result, $optionRow): array
1164+
private function addPriceData(array $result, array $optionRow): array
11621165
{
11631166
if (isset($optionRow['price'])) {
11641167
$percent_suffix = '';

dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Import/Product/Type/BundleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function testBundleImportWithMultipleStoreViews(): void
164164
foreach ($product->getStoreIds() as $storeId) {
165165
$bundleOptionCollection = $productRepository->get(self::TEST_PRODUCT_NAME, false, $storeId)
166166
->getExtensionAttributes()->getBundleProductOptions();
167-
$this->assertEquals(2, count($bundleOptionCollection));
167+
$this->assertCount(2, $bundleOptionCollection);
168168
$i++;
169169
foreach ($bundleOptionCollection as $optionKey => $option) {
170170
$this->assertEquals('checkbox', $option->getData('type'));

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/AbstractProductExportImportTestCase.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ protected function executeImportReplaceTest($skus, $skippedAttributes, $usePagin
321321

322322
while ($index > 0) {
323323
$index--;
324-
$productRepository->cleanCache();
325324
$newProduct = $productRepository->get($skus[$index], false, Store::DEFAULT_STORE_ID, true);
326325
// check original product is deleted
327326
$origProduct = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load($ids[$index]);

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Export/ProductTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,10 @@ public function testExportWithCustomOptions(): void
382382
}
383383

384384
/**
385-
* @param $exportedCustomOption
385+
* @param string $exportedCustomOption
386386
* @return array
387387
*/
388-
private function parseExportedCustomOption($exportedCustomOption)
388+
private function parseExportedCustomOption(string $exportedCustomOption): array
389389
{
390390
$customOptions = explode('|', $exportedCustomOption);
391391
$optionItems = [];

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected function setUp()
100100
protected $_assertOptionValues = [
101101
'title' => 'option_title',
102102
'price' => 'price',
103-
'sku' => 'sku'
103+
'sku' => 'sku',
104104
];
105105

106106
/**
@@ -350,7 +350,7 @@ public function testSaveCustomOptionsWithMultipleStoreViews()
350350
$storeCodes = [
351351
'admin',
352352
'default',
353-
'fixture_second_store'
353+
'fixture_second_store',
354354
];
355355
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
356356
$importFile = 'product_with_custom_options_and_multiple_store_views.csv';
@@ -553,7 +553,7 @@ public function testSaveDatetimeAttribute()
553553
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
554554
* @SuppressWarnings(PHPMD.NPathComplexity)
555555
*/
556-
protected function getExpectedOptionsData($pathToFile, $storeCode = ''): array
556+
protected function getExpectedOptionsData(string $pathToFile, string $storeCode = ''): array
557557
{
558558
$productData = $this->csvToArray(file_get_contents($pathToFile));
559559
$expectedOptionId = 0;
@@ -580,7 +580,7 @@ function ($input) {
580580
explode(',', $optionData)
581581
)
582582
);
583-
$option = call_user_func_array('array_merge', $option);
583+
$option = array_merge(...$option);
584584

585585
if (!empty($option['type']) && !empty($option['name'])) {
586586
$lastOptionKey = $option['type'] . '|' . $option['name'];

0 commit comments

Comments
 (0)