@@ -116,24 +116,37 @@ public function execute(
116
116
}
117
117
118
118
$ connection = $ this ->resourceConnection ->getConnection ();
119
- $ itemN = 0 ;
120
119
foreach ($ queryArguments as $ scopeId => $ productData ) {
120
+ $ storeViewItemN = [];
121
121
$ cursor = $ connection ->query (
122
122
$ this ->productMainQuery ->getQuery (\array_keys ($ productData ), $ scopeId ?: null )
123
123
);
124
124
125
125
while ($ row = $ cursor ->fetch ()) {
126
- $ itemN ++;
127
- $ mappedProducts [$ row ['storeViewCode ' ]][$ row ['productId ' ]] = $ row ;
128
- $ attributesData [$ row ['storeViewCode ' ]][$ row ['productId ' ]] = $ productData [$ row ['productId ' ]];
129
- if ($ itemN % $ metadata ->getBatchSize () == 0 ) {
130
- $ this ->processProducts ($ mappedProducts , $ attributesData , $ dataProcessorCallback , $ metadata );
131
- $ mappedProducts = [];
132
- $ attributesData = [];
126
+ $ storeViewCode = $ row ['storeViewCode ' ];
127
+ $ productId = $ row ['productId ' ];
128
+
129
+ if (!isset ($ storeViewItemN [$ storeViewCode ])) {
130
+ $ storeViewItemN [$ storeViewCode ] = 0 ;
131
+ }
132
+ $ storeViewItemN [$ storeViewCode ]++;
133
+
134
+ $ mappedProducts [$ storeViewCode ][$ productId ] = $ row ;
135
+ $ attributesData [$ storeViewCode ][$ productId ] = $ productData [$ productId ];
136
+
137
+ if ($ storeViewItemN [$ storeViewCode ] % $ metadata ->getBatchSize () == 0
138
+ || count ($ mappedProducts ) % $ metadata ->getBatchSize () == 0 ) {
139
+ $ this ->processProducts (
140
+ $ mappedProducts ,
141
+ $ attributesData ,
142
+ $ dataProcessorCallback ,
143
+ $ storeViewCode
144
+ );
145
+ unset($ mappedProducts [$ storeViewCode ], $ attributesData [$ storeViewCode ]);
133
146
}
134
147
}
135
148
}
136
- if ($ itemN === 0 ) {
149
+ if (empty ( $ storeViewItemN ) ) {
137
150
$ productsIds = \implode (', ' , \array_unique (\array_column ($ arguments , 'productId ' )));
138
151
$ scopes = \implode (', ' , \array_unique (\array_column ($ arguments , 'scopeId ' )));
139
152
$ this ->logger ->info (
@@ -145,7 +158,7 @@ public function execute(
145
158
)
146
159
);
147
160
} else {
148
- $ this ->processProducts ($ mappedProducts , $ attributesData , $ dataProcessorCallback, $ metadata );
161
+ $ this ->processProducts ($ mappedProducts , $ attributesData , $ dataProcessorCallback );
149
162
}
150
163
}
151
164
@@ -166,25 +179,28 @@ public function get(array $values) : array
166
179
* @param array $mappedProducts
167
180
* @param array $attributesData
168
181
* @param callable $dataProcessorCallback
169
- * @param FeedIndexMetadata $metadata
182
+ * @param string|null $storeViewCode
170
183
* @return void
171
184
* @throws UnableRetrieveData
172
185
*/
173
186
private function processProducts (
174
187
array $ mappedProducts ,
175
188
array $ attributesData ,
176
189
callable $ dataProcessorCallback ,
177
- FeedIndexMetadata $ metadata
190
+ string $ storeViewCode = null
178
191
): void {
179
192
$ output = [];
180
-
181
- foreach ($ mappedProducts as $ storeCode => $ products ) {
182
- $ output [] = \array_map (function ($ row ) {
183
- return $ this ->formatter ->format ($ row );
184
- }, \array_replace_recursive (
185
- $ products ,
186
- $ this ->entityEavAttributesResolver ->resolve ($ attributesData [$ storeCode ], $ storeCode )
187
- ));
193
+ if (null === $ storeViewCode ) {
194
+ foreach ($ mappedProducts as $ mappedStoreViewCode => $ products ) {
195
+ $ this ->formatOutput ($ products , $ attributesData [$ mappedStoreViewCode ], $ output , $ mappedStoreViewCode );
196
+ }
197
+ } else {
198
+ $ this ->formatOutput (
199
+ $ mappedProducts [$ storeViewCode ],
200
+ $ attributesData [$ storeViewCode ],
201
+ $ output ,
202
+ $ storeViewCode
203
+ );
188
204
}
189
205
190
206
$ errorEntityIds = [];
@@ -205,4 +221,28 @@ private function processProducts(
205
221
206
222
$ dataProcessorCallback ($ this ->get (\array_merge (...$ output )));
207
223
}
224
+
225
+ /**
226
+ * Format output
227
+ *
228
+ * @param array $products
229
+ * @param array $attributesData
230
+ * @param array $output
231
+ * @param string $storeViewCode
232
+ * @return void
233
+ * @throws UnableRetrieveData
234
+ */
235
+ private function formatOutput (
236
+ array $ products ,
237
+ array $ attributesData ,
238
+ array &$ output ,
239
+ string $ storeViewCode
240
+ ): void {
241
+ $ output [] = \array_map (function ($ row ) {
242
+ return $ this ->formatter ->format ($ row );
243
+ }, \array_replace_recursive (
244
+ $ products ,
245
+ $ this ->entityEavAttributesResolver ->resolve ($ attributesData , $ storeViewCode )
246
+ ));
247
+ }
208
248
}
0 commit comments