Skip to content

Commit 769c48d

Browse files
author
Oleksandr Iegorov
committed
MC-21727: It is impossible to remove Related, Up-Sells and Cross-Sells products via the import procedure
1 parent 9fb7790 commit 769c48d

File tree

1 file changed

+34
-2
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import

1 file changed

+34
-2
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ protected function _initTypeModels()
11981198
// phpcs:disable Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
11991199
$this->_fieldsMap = array_merge($this->_fieldsMap, $model->getCustomFieldsMapping());
12001200
$this->_specialAttributes = array_merge($this->_specialAttributes, $model->getParticularAttributes());
1201-
// phpcs:enable
1201+
// phpcs:enable
12021202
}
12031203
$this->_initErrorTemplates();
12041204
// remove doubles
@@ -3060,6 +3060,7 @@ private function getValidationErrorLevel($sku): string
30603060
* @param int $nextLinkId
30613061
* @param array $positionAttrId
30623062
* @return void
3063+
* @throws LocalizedException
30633064
*/
30643065
private function processLinkBunches(
30653066
array $bunch,
@@ -3070,6 +3071,7 @@ private function processLinkBunches(
30703071
$productIds = [];
30713072
$linkRows = [];
30723073
$positionRows = [];
3074+
$linksToDelete = [];
30733075

30743076
$bunch = array_filter($bunch, [$this, 'isRowAllowedToImport'], ARRAY_FILTER_USE_BOTH);
30753077
foreach ($bunch as $rowData) {
@@ -3086,10 +3088,15 @@ function ($linkName) use ($rowData) {
30863088
);
30873089
foreach ($linkNameToId as $linkName => $linkId) {
30883090
$linkSkus = explode($this->getMultipleValueSeparator(), $rowData[$linkName . 'sku']);
3091+
//process empty value
3092+
if (!empty($linkSkus[0]) && $linkSkus[0] === Import::DEFAULT_EMPTY_ATTRIBUTE_VALUE_CONSTANT) {
3093+
$linksToDelete[$linkId][] = $productId;
3094+
continue;
3095+
}
3096+
30893097
$linkPositions = !empty($rowData[$linkName . 'position'])
30903098
? explode($this->getMultipleValueSeparator(), $rowData[$linkName . 'position'])
30913099
: [];
3092-
30933100
$linkSkus = array_filter(
30943101
$linkSkus,
30953102
function ($linkedSku) use ($sku) {
@@ -3098,6 +3105,7 @@ function ($linkedSku) use ($sku) {
30983105
&& strcasecmp($linkedSku, $sku) !== 0;
30993106
}
31003107
);
3108+
31013109
foreach ($linkSkus as $linkedKey => $linkedSku) {
31023110
$linkedId = $this->getProductLinkedId($linkedSku);
31033111
if ($linkedId == null) {
@@ -3129,9 +3137,33 @@ function ($linkedSku) use ($sku) {
31293137
}
31303138
}
31313139
}
3140+
$this->deleteProductsLinks($resource, $linksToDelete);
31323141
$this->saveLinksData($resource, $productIds, $linkRows, $positionRows);
31333142
}
31343143

3144+
/**
3145+
* Delete links
3146+
*
3147+
* @param Link $resource
3148+
* @param array $linksToDelete
3149+
* @return void
3150+
* @throws LocalizedException
3151+
*/
3152+
private function deleteProductsLinks(Link $resource, array $linksToDelete) {
3153+
if (!empty($linksToDelete) && Import::BEHAVIOR_APPEND === $this->getBehavior()) {
3154+
foreach ($linksToDelete as $linkTypeId => $productIds) {
3155+
if (!empty($productIds)) {
3156+
$whereLinkId = $this->_connection->quoteInto('link_type_id', $linkTypeId);
3157+
$whereProductId = $this->_connection->quoteInto('product_id IN (?)', array_unique($productIds));
3158+
$this->_connection->delete(
3159+
$resource->getMainTable(),
3160+
$whereLinkId . ' AND ' . $whereProductId
3161+
);
3162+
}
3163+
}
3164+
}
3165+
}
3166+
31353167
/**
31363168
* Fetches Product Links
31373169
*

0 commit comments

Comments
 (0)