Skip to content

Commit 9c11e76

Browse files
MC-34657: Related products are missing in some products when importing products
1 parent f571e90 commit 9c11e76

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public function saveLinks(
8989
$resource = $this->linkFactory->create();
9090
$mainTable = $resource->getMainTable();
9191
$positionAttrId = [];
92-
$nextLinkId = $this->resourceHelper->getNextAutoincrement($mainTable);
9392

9493
// pre-load 'position' attributes ID for each link type once
9594
foreach ($this->linkNameToId as $linkId) {
@@ -103,6 +102,7 @@ public function saveLinks(
103102
$positionAttrId[$linkId] = $importEntity->getConnection()->fetchOne($select, $bind);
104103
}
105104
while ($bunch = $dataSourceModel->getNextBunch()) {
105+
$nextLinkId = $this->resourceHelper->getNextAutoincrement($mainTable);
106106
$this->processLinkBunches($importEntity, $linkField, $bunch, $resource, $nextLinkId, $positionAttrId);
107107
}
108108
}

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,4 +3127,73 @@ public function testCheckDoubleImportOfProducts()
31273127
$productsAfterSecondImport = $this->productRepository->getList($searchCriteria)->getItems();
31283128
$this->assertCount(3, $productsAfterSecondImport);
31293129
}
3130+
3131+
/**
3132+
* Checks that product related links added for all bunches properly after products import
3133+
*/
3134+
public function testImportProductsWithLinksInDifferentBunches()
3135+
{
3136+
$this->importedProducts = [
3137+
'simple1',
3138+
'simple2',
3139+
'simple3',
3140+
'simple4',
3141+
'simple5',
3142+
'simple6',
3143+
];
3144+
$importExportData = $this->getMockBuilder(Data::class)
3145+
->disableOriginalConstructor()
3146+
->getMock();
3147+
$importExportData->expects($this->atLeastOnce())
3148+
->method('getBunchSize')
3149+
->willReturn(5);
3150+
$this->_model = $this->objectManager->create(
3151+
\Magento\CatalogImportExport\Model\Import\Product::class,
3152+
['importExportData' => $importExportData]
3153+
);
3154+
$linksData = [
3155+
'related' => [
3156+
'simple1' => '2',
3157+
'simple2' => '1'
3158+
]
3159+
];
3160+
$pathToFile = __DIR__ . '/_files/products_to_import_with_related.csv';
3161+
$filesystem = $this->objectManager->create(Filesystem::class);
3162+
3163+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
3164+
$source = $this->objectManager->create(
3165+
Csv::class,
3166+
[
3167+
'file' => $pathToFile,
3168+
'directory' => $directory
3169+
]
3170+
);
3171+
$errors = $this->_model->setSource($source)
3172+
->setParameters(
3173+
[
3174+
'behavior' => Import::BEHAVIOR_APPEND,
3175+
'entity' => 'catalog_product'
3176+
]
3177+
)
3178+
->validateData();
3179+
3180+
$this->assertTrue($errors->getErrorsCount() == 0);
3181+
$this->_model->importData();
3182+
3183+
$resource = $this->objectManager->get(ProductResource::class);
3184+
$productId = $resource->getIdBySku('simple6');
3185+
/** @var Product $product */
3186+
$product = $this->objectManager->create(Product::class);
3187+
$product->load($productId);
3188+
$productLinks = [
3189+
'related' => $product->getRelatedProducts()
3190+
];
3191+
$importedProductLinks = [];
3192+
foreach ($productLinks as $linkType => $linkedProducts) {
3193+
foreach ($linkedProducts as $linkedProductData) {
3194+
$importedProductLinks[$linkType][$linkedProductData->getSku()] = $linkedProductData->getPosition();
3195+
}
3196+
}
3197+
$this->assertEquals($linksData, $importedProductLinks);
3198+
}
31303199
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sku,product_type,store_view_code,name,price,qty,attribute_set_code,related_skus,related_position
2+
simple1,simple,,simple 1,25,10,Default,,
3+
simple2,simple,,simple 2,34,10,Default,,
4+
simple3,simple,,simple 3,58,10,Default,"simple1,simple2","1,2"
5+
simple4,simple,,simple 4,67,10,Default,"simple1,simple2","2,1"
6+
simple5,simple,,simple 5,58,10,Default,"simple1,simple2","1,2"
7+
simple6,simple,,simple 6,67,10,Default,"simple1,simple2","2,1"

0 commit comments

Comments
 (0)