Skip to content

Commit e8e164f

Browse files
committed
#23191 Port delete processing from commit 769c48d after merge
1 parent dd124e8 commit e8e164f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ private function processLinkBunches(
140140
$productIds = [];
141141
$linkRows = [];
142142
$positionRows = [];
143+
$linksToDelete = [];
143144

144145
$bunch = array_filter($bunch, [$importEntity, 'isRowAllowedToImport'], ARRAY_FILTER_USE_BOTH);
145146
foreach ($bunch as $rowData) {
@@ -156,6 +157,12 @@ function ($linkName) use ($rowData) {
156157
);
157158
foreach ($linkNameToId as $linkName => $linkId) {
158159
$linkSkus = explode($importEntity->getMultipleValueSeparator(), $rowData[$linkName . 'sku']);
160+
161+
//process empty value
162+
if (!empty($linkSkus[0]) && $linkSkus[0] === $importEntity->getEmptyAttributeValueConstant()) {
163+
$linksToDelete[$linkId][] = $productId;
164+
continue;
165+
}
159166
$linkPositions = ! empty($rowData[$linkName . 'position'])
160167
? explode($importEntity->getMultipleValueSeparator(), $rowData[$linkName . 'position'])
161168
: [];
@@ -203,9 +210,43 @@ function ($linkedSku) use ($sku, $importEntity) {
203210
}
204211
}
205212
}
213+
214+
$this->deleteProductsLinks($importEntity, $resource, $linksToDelete);
206215
$this->saveLinksData($importEntity, $resource, $productIds, $linkRows, $positionRows);
207216
}
208217

218+
/**
219+
* Delete links
220+
*
221+
* @param Product $importEntity
222+
* @param Link $resource
223+
* @param array $linksToDelete
224+
* @return void
225+
* @throws LocalizedException
226+
*/
227+
private function deleteProductsLinks(
228+
Product $importEntity,
229+
Link $resource,
230+
array $linksToDelete
231+
)
232+
{
233+
if (!empty($linksToDelete) && Import::BEHAVIOR_APPEND === $importEntity->getBehavior()) {
234+
foreach ($linksToDelete as $linkTypeId => $productIds) {
235+
if (!empty($productIds)) {
236+
$whereLinkId = $importEntity->getConnection()->quoteInto('link_type_id', $linkTypeId);
237+
$whereProductId = $importEntity->getConnection()->quoteInto(
238+
'product_id IN (?)',
239+
array_unique($productIds)
240+
);
241+
$importEntity->getConnection()->delete(
242+
$resource->getMainTable(),
243+
$whereLinkId . ' AND ' . $whereProductId
244+
);
245+
}
246+
}
247+
}
248+
}
249+
209250
/**
210251
* Check if product exists for specified SKU
211252
*

0 commit comments

Comments
 (0)