@@ -113,7 +113,7 @@ public function __construct(
113
113
}
114
114
115
115
/**
116
- * Prepare and save quotes in database
116
+ * Prepare and save quotes in database.
117
117
*
118
118
* @throws \Exception
119
119
* @return void
@@ -135,13 +135,12 @@ public function generateQuotes()
135
135
$ maxItemsPerOrder
136
136
);
137
137
$ this ->productStubData = $ this ->prepareProductsForQuote ();
138
-
139
138
$ this ->prepareQueryTemplates ();
140
139
141
140
$ entityId = $ this ->getMaxEntityId ('quote ' , \Magento \Quote \Model \ResourceModel \Quote::class, 'entity_id ' );
142
141
$ quoteQty = $ this ->config ->getExistsQuoteQuantity ();
143
142
$ batchNumber = 0 ;
144
- while ($ quoteQty <= $ this ->config ->getRequiredQuoteQuantity ()) {
143
+ while ($ quoteQty < $ this ->config ->getRequiredQuoteQuantity ()) {
145
144
$ entityId ++;
146
145
$ batchNumber ++;
147
146
$ quoteQty ++;
@@ -171,7 +170,7 @@ public function generateQuotes()
171
170
}
172
171
173
172
/**
174
- * Save quote and quote items
173
+ * Save quote and quote items.
175
174
*
176
175
* @param int $entityId
177
176
* @param \Generator $itemIdSequence
@@ -215,30 +214,18 @@ private function saveQuoteWithQuoteItems($entityId, \Generator $itemIdSequence)
215
214
$ itemIdSequence ->next ();
216
215
}
217
216
218
- $ type = Configurable::TYPE_CODE ;
219
- for ($ i = 0 ; $ i < $ productCount [$ type ]; $ i ++) {
220
- // Generate parent item
221
- $ parentItemId = $ itemIdSequence ->current ();
222
- $ this ->saveParentItemConfigurableData ($ entityId , $ i , $ parentItemId , Configurable::TYPE_CODE , $ quote );
223
- $ itemIdSequence ->next ();
224
-
225
- // Generate child item
226
- $ itemId = $ itemIdSequence ->current ();
227
- $ this ->saveChildItemConfigurable ($ entityId , $ i , $ itemId , $ parentItemId , Configurable::TYPE_CODE , $ quote );
228
- $ itemIdSequence ->next ();
229
- }
230
-
231
- $ type = QuoteConfiguration::BIG_CONFIGURABLE_TYPE ;
232
- for ($ i = 0 ; $ i < $ productCount [$ type ]; $ i ++) {
233
- // Generate parent item
234
- $ parentItemId = $ itemIdSequence ->current ();
235
- $ this ->saveParentItemConfigurableData ($ entityId , $ i , $ parentItemId , $ type , $ quote );
236
- $ itemIdSequence ->next ();
237
-
238
- // Generate child item
239
- $ itemId = $ itemIdSequence ->current ();
240
- $ this ->saveChildItemConfigurable ($ entityId , $ i , $ itemId , $ parentItemId , $ type , $ quote );
241
- $ itemIdSequence ->next ();
217
+ foreach ([Configurable::TYPE_CODE , QuoteConfiguration::BIG_CONFIGURABLE_TYPE ] as $ type ) {
218
+ for ($ i = 0 ; $ i < $ productCount [$ type ]; $ i ++) {
219
+ // Generate parent item
220
+ $ parentItemId = $ itemIdSequence ->current ();
221
+ $ this ->saveParentItemConfigurableData ($ entityId , $ i , $ parentItemId , $ type , $ quote );
222
+ $ itemIdSequence ->next ();
223
+
224
+ // Generate child item
225
+ $ itemId = $ itemIdSequence ->current ();
226
+ $ this ->saveChildItemConfigurable ($ entityId , $ i , $ itemId , $ parentItemId , $ type , $ quote );
227
+ $ itemIdSequence ->next ();
228
+ }
242
229
}
243
230
}
244
231
@@ -351,23 +338,23 @@ private function saveChildItemConfigurable($entityId, $index, $itemId, $parentIt
351
338
/**
352
339
* Get store id for quote item by product index.
353
340
*
354
- * @param int $index
341
+ * @param int $entityId
355
342
* @return int
356
343
*/
357
- private function getStubProductStoreId ($ index )
344
+ private function getStubProductStoreId ($ entityId )
358
345
{
359
- return $ this ->productStubData [$ index % count ( $ this ->productStubData )][0 ];
346
+ return $ this ->productStubData [$ this ->getProductStubIndex ( $ entityId )][0 ];
360
347
}
361
348
362
349
/**
363
350
* Get store name for quote item by product index.
364
351
*
365
- * @param int $index
352
+ * @param int $entityId
366
353
* @return string
367
354
*/
368
- private function getStubProductStoreName ($ index )
355
+ private function getStubProductStoreName ($ entityId )
369
356
{
370
- return $ this ->productStubData [$ index % count ( $ this ->productStubData )][1 ];
357
+ return $ this ->productStubData [$ this ->getProductStubIndex ( $ entityId )][1 ];
371
358
}
372
359
373
360
/**
@@ -380,7 +367,7 @@ private function getStubProductStoreName($index)
380
367
*/
381
368
private function getStubProductId ($ entityId , $ index , $ type )
382
369
{
383
- return $ this ->productStubData [$ entityId % count ( $ this ->productStubData )][2 ][$ type ][$ index ]['id ' ];
370
+ return $ this ->productStubData [$ this ->getProductStubIndex ( $ entityId )][2 ][$ type ][$ index ]['id ' ];
384
371
}
385
372
386
373
/**
@@ -393,7 +380,7 @@ private function getStubProductId($entityId, $index, $type)
393
380
*/
394
381
private function getStubProductSku ($ entityId , $ index , $ type )
395
382
{
396
- return $ this ->productStubData [$ entityId % count ( $ this ->productStubData )][2 ][$ type ][$ index ]['sku ' ];
383
+ return $ this ->productStubData [$ this ->getProductStubIndex ( $ entityId )][2 ][$ type ][$ index ]['sku ' ];
397
384
}
398
385
399
386
/**
@@ -406,7 +393,7 @@ private function getStubProductSku($entityId, $index, $type)
406
393
*/
407
394
private function getStubProductName ($ entityId , $ index , $ type )
408
395
{
409
- return $ this ->productStubData [$ entityId % count ( $ this ->productStubData )][2 ][$ type ][$ index ]['name ' ];
396
+ return $ this ->productStubData [$ this ->getProductStubIndex ( $ entityId )][2 ][$ type ][$ index ]['name ' ];
410
397
}
411
398
412
399
/**
@@ -419,7 +406,7 @@ private function getStubProductName($entityId, $index, $type)
419
406
*/
420
407
private function getStubProductBuyRequest ($ entityId , $ index , $ type )
421
408
{
422
- return $ this ->productStubData [$ entityId % count ( $ this ->productStubData )][2 ][$ type ][$ index ]['buyRequest ' ];
409
+ return $ this ->productStubData [$ this ->getProductStubIndex ( $ entityId )][2 ][$ type ][$ index ]['buyRequest ' ];
423
410
}
424
411
425
412
/**
@@ -432,7 +419,7 @@ private function getStubProductBuyRequest($entityId, $index, $type)
432
419
*/
433
420
private function getStubProductChildBuyRequest ($ entityId , $ index , $ type )
434
421
{
435
- return $ this ->productStubData [$ entityId % count ( $ this ->productStubData )][2 ][$ type ][$ index ]['childBuyRequest ' ];
422
+ return $ this ->productStubData [$ this ->getProductStubIndex ( $ entityId )][2 ][$ type ][$ index ]['childBuyRequest ' ];
436
423
}
437
424
438
425
/**
@@ -445,7 +432,20 @@ private function getStubProductChildBuyRequest($entityId, $index, $type)
445
432
*/
446
433
private function getStubProductChildId ($ entityId , $ index , $ type )
447
434
{
448
- return $ this ->productStubData [$ entityId % count ($ this ->productStubData )][2 ][$ type ][$ index ]['childId ' ];
435
+ return $ this ->productStubData [$ this ->getProductStubIndex ($ entityId )][2 ][$ type ][$ index ]['childId ' ];
436
+ }
437
+
438
+ /**
439
+ * Get index of item in product stub array.
440
+ *
441
+ * @param int $entityId
442
+ * @return int
443
+ */
444
+ private function getProductStubIndex ($ entityId )
445
+ {
446
+ $ storeCount = count ($ this ->productStubData );
447
+ $ qty = intdiv ($ this ->config ->getRequiredQuoteQuantity (), $ storeCount );
448
+ return intdiv ($ entityId , $ qty ) % $ storeCount ;
449
449
}
450
450
451
451
/**
@@ -476,6 +476,7 @@ private function getAddressDataFixture()
476
476
private function prepareProductsForQuote ()
477
477
{
478
478
$ result = [];
479
+
479
480
foreach ($ this ->storeManager ->getStores () as $ store ) {
480
481
$ productsResult = [];
481
482
$ this ->storeManager ->setCurrentStore ($ store ->getId ());
@@ -485,23 +486,21 @@ private function prepareProductsForQuote()
485
486
$ this ->getProductIds ($ store , Type::TYPE_SIMPLE , $ this ->config ->getSimpleCountTo ())
486
487
);
487
488
}
488
- if ($ this ->config ->getConfigurableCountTo () > 0 ) {
489
- $ productsResult [Configurable::TYPE_CODE ] = $ this ->prepareConfigurableProducts (
490
- $ this ->getProductIds (
491
- $ store ,
492
- Configurable::TYPE_CODE ,
493
- $ this ->config ->getConfigurableCountTo ()
494
- )
495
- );
496
- }
497
- if ($ this ->config ->getBigConfigurableCountTo () > 0 ) {
498
- $ productsResult [QuoteConfiguration::BIG_CONFIGURABLE_TYPE ] = $ this ->prepareConfigurableProducts (
499
- $ this ->getProductIds (
500
- $ store ,
501
- QuoteConfiguration::BIG_CONFIGURABLE_TYPE ,
502
- $ this ->config ->getBigConfigurableCountTo ()
503
- )
504
- );
489
+ $ configurables = [
490
+ Configurable::TYPE_CODE => $ this ->config ->getConfigurableCountTo (),
491
+ QuoteConfiguration::BIG_CONFIGURABLE_TYPE => $ this ->config ->getBigConfigurableCountTo (),
492
+ ];
493
+
494
+ foreach ($ configurables as $ type => $ qty ) {
495
+ if ($ qty > 0 ) {
496
+ $ productsResult [$ type ] = $ this ->prepareConfigurableProducts (
497
+ $ this ->getProductIds (
498
+ $ store ,
499
+ $ type ,
500
+ $ qty
501
+ )
502
+ );
503
+ }
505
504
}
506
505
507
506
$ result [] = [
@@ -514,6 +513,7 @@ private function prepareProductsForQuote()
514
513
$ productsResult
515
514
];
516
515
}
516
+
517
517
return $ result ;
518
518
}
519
519
0 commit comments