@@ -95,16 +95,16 @@ public function saveData()
9595 if ($ this ->_type != $ rowData [Product::COL_TYPE ]) {
9696 continue ;
9797 }
98- $ associatedSkusQty = isset ( $ rowData ['associated_skus ' ]) ? $ rowData [ ' associated_skus ' ] : null ;
98+ $ associatedSkusQty = $ rowData ['associated_skus ' ] ?? null ;
9999 if (!$ this ->_entityModel ->isRowAllowedToImport ($ rowData , $ rowNum ) || empty ($ associatedSkusQty )) {
100100 continue ;
101101 }
102- $ associatedSkusAndQtyPairs = explode (Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR , $ associatedSkusQty );
102+
103+ $ associatedSkusAndQtyPairs = $ this ->normalizeSkusAndQty ($ associatedSkusQty );
104+
103105 $ position = 0 ;
104- foreach ($ associatedSkusAndQtyPairs as $ associatedSkuAndQty ) {
106+ foreach ($ associatedSkusAndQtyPairs as $ associatedSku => $ qty ) {
105107 ++$ position ;
106- $ associatedSkuAndQty = explode (self ::SKU_QTY_DELIMITER , $ associatedSkuAndQty );
107- $ associatedSku = isset ($ associatedSkuAndQty [0 ]) ? strtolower (trim ($ associatedSkuAndQty [0 ])) : null ;
108108 if (isset ($ newSku [$ associatedSku ]) &&
109109 in_array ($ newSku [$ associatedSku ]['type_id ' ], $ this ->allowedProductTypes )
110110 ) {
@@ -116,6 +116,7 @@ public function saveData()
116116 } else {
117117 continue ;
118118 }
119+
119120 $ scope = $ this ->_entityModel ->getRowScope ($ rowData );
120121 if (Product::SCOPE_DEFAULT == $ scope ) {
121122 $ productData = $ newSku [strtolower ($ rowData [Product::COL_SKU ])];
@@ -124,11 +125,10 @@ public function saveData()
124125 $ rowData [$ colAttrSet ] = $ productData ['attr_set_code ' ];
125126 $ rowData [Product::COL_TYPE ] = $ productData ['type_id ' ];
126127 }
127- $ productId = $ productData [$ this ->getProductEntityLinkField ()];
128128
129+ $ productId = $ productData [$ this ->getProductEntityLinkField ()];
129130 $ linksData ['product_ids ' ][$ productId ] = true ;
130131 $ linksData ['relation ' ][] = ['parent_id ' => $ productId , 'child_id ' => $ linkedProductId ];
131- $ qty = empty ($ associatedSkuAndQty [1 ]) ? 0 : trim ($ associatedSkuAndQty [1 ]);
132132 $ linksData ['attr_product_ids ' ][$ productId ] = true ;
133133 $ linksData ['position ' ]["{$ productId } {$ linkedProductId }" ] = [
134134 'product_link_attribute_id ' => $ attributes ['position ' ]['id ' ],
@@ -148,6 +148,33 @@ public function saveData()
148148 return $ this ;
149149 }
150150
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+
151178 /**
152179 * Get product entity identifier field
153180 *
0 commit comments