@@ -95,16 +95,16 @@ public function saveData()
95
95
if ($ this ->_type != $ rowData [Product::COL_TYPE ]) {
96
96
continue ;
97
97
}
98
- $ associatedSkusQty = isset ( $ rowData ['associated_skus ' ]) ? $ rowData [ ' associated_skus ' ] : null ;
98
+ $ associatedSkusQty = $ rowData ['associated_skus ' ] ?? null ;
99
99
if (!$ this ->_entityModel ->isRowAllowedToImport ($ rowData , $ rowNum ) || empty ($ associatedSkusQty )) {
100
100
continue ;
101
101
}
102
- $ associatedSkusAndQtyPairs = explode (Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR , $ associatedSkusQty );
102
+
103
+ $ associatedSkusAndQtyPairs = $ this ->normalizeSkusAndQty ($ associatedSkusQty );
104
+
103
105
$ position = 0 ;
104
- foreach ($ associatedSkusAndQtyPairs as $ associatedSkuAndQty ) {
106
+ foreach ($ associatedSkusAndQtyPairs as $ associatedSku => $ qty ) {
105
107
++$ position ;
106
- $ associatedSkuAndQty = explode (self ::SKU_QTY_DELIMITER , $ associatedSkuAndQty );
107
- $ associatedSku = isset ($ associatedSkuAndQty [0 ]) ? strtolower (trim ($ associatedSkuAndQty [0 ])) : null ;
108
108
if (isset ($ newSku [$ associatedSku ]) &&
109
109
in_array ($ newSku [$ associatedSku ]['type_id ' ], $ this ->allowedProductTypes )
110
110
) {
@@ -116,6 +116,7 @@ public function saveData()
116
116
} else {
117
117
continue ;
118
118
}
119
+
119
120
$ scope = $ this ->_entityModel ->getRowScope ($ rowData );
120
121
if (Product::SCOPE_DEFAULT == $ scope ) {
121
122
$ productData = $ newSku [strtolower ($ rowData [Product::COL_SKU ])];
@@ -124,11 +125,10 @@ public function saveData()
124
125
$ rowData [$ colAttrSet ] = $ productData ['attr_set_code ' ];
125
126
$ rowData [Product::COL_TYPE ] = $ productData ['type_id ' ];
126
127
}
127
- $ productId = $ productData [$ this ->getProductEntityLinkField ()];
128
128
129
+ $ productId = $ productData [$ this ->getProductEntityLinkField ()];
129
130
$ linksData ['product_ids ' ][$ productId ] = true ;
130
131
$ linksData ['relation ' ][] = ['parent_id ' => $ productId , 'child_id ' => $ linkedProductId ];
131
- $ qty = empty ($ associatedSkuAndQty [1 ]) ? 0 : trim ($ associatedSkuAndQty [1 ]);
132
132
$ linksData ['attr_product_ids ' ][$ productId ] = true ;
133
133
$ linksData ['position ' ]["{$ productId } {$ linkedProductId }" ] = [
134
134
'product_link_attribute_id ' => $ attributes ['position ' ]['id ' ],
@@ -148,6 +148,33 @@ public function saveData()
148
148
return $ this ;
149
149
}
150
150
151
+ /**
152
+ * Normalize SKU-Quantity pairs.
153
+ *
154
+ * @param array|string $associatedSkusQty
155
+ * @return array
156
+ */
157
+ private function normalizeSkusAndQty (array |string $ associatedSkusQty ): array
158
+ {
159
+ $ normalizedSkusAndQty = [];
160
+
161
+ if (is_string ($ associatedSkusQty )) {
162
+ $ associatedSkusQtyTemp = explode (Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR , $ associatedSkusQty );
163
+ foreach ($ associatedSkusQtyTemp as $ skuQty ) {
164
+ $ skuQtyPair = explode (self ::SKU_QTY_DELIMITER , $ skuQty );
165
+ $ associatedSku = strtolower (trim ($ skuQtyPair [0 ]));
166
+ $ associatedQty = empty ($ skuQtyPair [1 ]) ? 0 : trim ($ skuQtyPair [1 ]);
167
+ $ normalizedSkusAndQty [$ associatedSku ] = $ associatedQty ;
168
+ }
169
+ } elseif (is_array ($ associatedSkusQty )) {
170
+ foreach ($ associatedSkusQty as $ associatedSku => $ associatedQty ) {
171
+ $ normalizedSkusAndQty [strtolower (trim ($ associatedSku ))] = $ associatedQty ;
172
+ }
173
+ }
174
+
175
+ return $ normalizedSkusAndQty ;
176
+ }
177
+
151
178
/**
152
179
* Get product entity identifier field
153
180
*
0 commit comments