@@ -1198,7 +1198,7 @@ protected function _initTypeModels()
1198
1198
// phpcs:disable Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
1199
1199
$ this ->_fieldsMap = array_merge ($ this ->_fieldsMap , $ model ->getCustomFieldsMapping ());
1200
1200
$ this ->_specialAttributes = array_merge ($ this ->_specialAttributes , $ model ->getParticularAttributes ());
1201
- // phpcs:enable
1201
+ // phpcs:enable
1202
1202
}
1203
1203
$ this ->_initErrorTemplates ();
1204
1204
// remove doubles
@@ -3060,6 +3060,7 @@ private function getValidationErrorLevel($sku): string
3060
3060
* @param int $nextLinkId
3061
3061
* @param array $positionAttrId
3062
3062
* @return void
3063
+ * @throws LocalizedException
3063
3064
*/
3064
3065
private function processLinkBunches (
3065
3066
array $ bunch ,
@@ -3070,6 +3071,7 @@ private function processLinkBunches(
3070
3071
$ productIds = [];
3071
3072
$ linkRows = [];
3072
3073
$ positionRows = [];
3074
+ $ linksToDelete = [];
3073
3075
3074
3076
$ bunch = array_filter ($ bunch , [$ this , 'isRowAllowedToImport ' ], ARRAY_FILTER_USE_BOTH );
3075
3077
foreach ($ bunch as $ rowData ) {
@@ -3086,10 +3088,15 @@ function ($linkName) use ($rowData) {
3086
3088
);
3087
3089
foreach ($ linkNameToId as $ linkName => $ linkId ) {
3088
3090
$ 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
+
3089
3097
$ linkPositions = !empty ($ rowData [$ linkName . 'position ' ])
3090
3098
? explode ($ this ->getMultipleValueSeparator (), $ rowData [$ linkName . 'position ' ])
3091
3099
: [];
3092
-
3093
3100
$ linkSkus = array_filter (
3094
3101
$ linkSkus ,
3095
3102
function ($ linkedSku ) use ($ sku ) {
@@ -3098,6 +3105,7 @@ function ($linkedSku) use ($sku) {
3098
3105
&& strcasecmp ($ linkedSku , $ sku ) !== 0 ;
3099
3106
}
3100
3107
);
3108
+
3101
3109
foreach ($ linkSkus as $ linkedKey => $ linkedSku ) {
3102
3110
$ linkedId = $ this ->getProductLinkedId ($ linkedSku );
3103
3111
if ($ linkedId == null ) {
@@ -3129,9 +3137,33 @@ function ($linkedSku) use ($sku) {
3129
3137
}
3130
3138
}
3131
3139
}
3140
+ $ this ->deleteProductsLinks ($ resource , $ linksToDelete );
3132
3141
$ this ->saveLinksData ($ resource , $ productIds , $ linkRows , $ positionRows );
3133
3142
}
3134
3143
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
+
3135
3167
/**
3136
3168
* Fetches Product Links
3137
3169
*
0 commit comments