@@ -326,6 +326,20 @@ public function save($product)
326
326
return $ this ;
327
327
}
328
328
329
+ protected function getProductInfo (\Magento \Framework \Object $ buyRequest , $ product )
330
+ {
331
+ $ productsInfo = $ buyRequest ->getSuperGroup () ?: [];
332
+
333
+ $ associatedProducts = $ this ->getAssociatedProducts ($ product );
334
+ foreach ($ associatedProducts as $ subProduct ) {
335
+ if (!isset ($ productsInfo [$ subProduct ->getId ()])) {
336
+ $ productsInfo [$ subProduct ->getId ()] = intval ($ subProduct ->getQty ());
337
+ }
338
+ }
339
+
340
+ return $ productsInfo ;
341
+ }
342
+
329
343
/**
330
344
* Prepare product and its configuration to be added to some products list.
331
345
* Perform standard preparation process and add logic specific to Grouped product type.
@@ -338,67 +352,63 @@ public function save($product)
338
352
*/
339
353
protected function _prepareProduct (\Magento \Framework \Object $ buyRequest , $ product , $ processMode )
340
354
{
341
- $ productsInfo = $ buyRequest ->getSuperGroup ();
355
+ $ products = [];
356
+ $ associatedProductsInfo = [];
357
+ $ productsInfo = $ this ->getProductInfo ($ buyRequest , $ product );
342
358
$ isStrictProcessMode = $ this ->_isStrictProcessMode ($ processMode );
359
+ $ associatedProducts = !$ isStrictProcessMode || !empty ($ productsInfo )
360
+ ? $ this ->getAssociatedProducts ($ product )
361
+ : false ;
343
362
344
- if (!$ isStrictProcessMode || !empty ($ productsInfo ) && is_array ($ productsInfo )) {
345
- $ products = [];
346
- $ associatedProductsInfo = [];
347
- $ associatedProducts = $ this ->getAssociatedProducts ($ product );
348
- if ($ associatedProducts || !$ isStrictProcessMode ) {
349
- foreach ($ associatedProducts as $ subProduct ) {
350
- $ subProductId = $ subProduct ->getId ();
351
- if (isset ($ productsInfo [$ subProductId ])) {
352
- $ qty = $ productsInfo [$ subProductId ];
353
- if (!empty ($ qty ) && is_numeric ($ qty )) {
354
- $ _result = $ subProduct ->getTypeInstance ()->_prepareProduct (
355
- $ buyRequest ,
356
- $ subProduct ,
357
- $ processMode
358
- );
359
- if (is_string ($ _result ) && !is_array ($ _result )) {
360
- return $ _result ;
361
- }
362
-
363
- if (!isset ($ _result [0 ])) {
364
- return __ ('We cannot process the item. ' )->render ();
365
- }
366
-
367
- if ($ isStrictProcessMode ) {
368
- $ _result [0 ]->setCartQty ($ qty );
369
- $ _result [0 ]->addCustomOption (
370
- 'info_buyRequest ' ,
371
- serialize (
372
- [
373
- 'super_product_config ' => [
374
- 'product_type ' => self ::TYPE_CODE ,
375
- 'product_id ' => $ product ->getId (),
376
- ],
377
- ]
378
- )
379
- );
380
- $ products [] = $ _result [0 ];
381
- } else {
382
- $ associatedProductsInfo [] = [$ subProductId => $ qty ];
383
- $ product ->addCustomOption ('associated_product_ ' . $ subProductId , $ qty );
384
- }
385
- }
386
- }
387
- }
363
+ if ($ associatedProducts && empty ($ productsInfo )) {
364
+ return __ ('Please specify the quantity of product(s). ' )->render ();
365
+ }
366
+
367
+ foreach ($ associatedProducts as $ subProduct ) {
368
+ $ qty = $ productsInfo [$ subProduct ->getId ()];
369
+ if (!is_numeric ($ qty )) {
370
+ continue ;
388
371
}
389
372
390
- if (!$ isStrictProcessMode || count ($ associatedProductsInfo )) {
391
- $ product ->addCustomOption ('product_type ' , self ::TYPE_CODE , $ product );
392
- $ product ->addCustomOption ('info_buyRequest ' , serialize ($ buyRequest ->getData ()));
373
+ $ _result = $ subProduct ->getTypeInstance ()->_prepareProduct ($ buyRequest , $ subProduct ,$ processMode );
393
374
394
- $ products [] = $ product ;
375
+ if (is_string ($ _result )) {
376
+ return $ _result ;
377
+ } elseif (!isset ($ _result [0 ])) {
378
+ return __ ('Cannot process the item. ' )->render ();
395
379
}
396
380
397
- if (count ($ products )) {
398
- return $ products ;
381
+ if ($ isStrictProcessMode ) {
382
+ $ _result [0 ]->setCartQty ($ qty );
383
+ $ _result [0 ]->addCustomOption (
384
+ 'info_buyRequest ' ,
385
+ serialize (
386
+ [
387
+ 'super_product_config ' => [
388
+ 'product_type ' => self ::TYPE_CODE ,
389
+ 'product_id ' => $ product ->getId (),
390
+ ],
391
+ ]
392
+ )
393
+ );
394
+ $ products [] = $ _result [0 ];
395
+ } else {
396
+ $ associatedProductsInfo [] = [$ subProduct ->getId () => $ qty ];
397
+ $ product ->addCustomOption ('associated_product_ ' . $ subProduct ->getId (), $ qty );
399
398
}
400
399
}
401
400
401
+ if (!$ isStrictProcessMode || count ($ associatedProductsInfo )) {
402
+ $ product ->addCustomOption ('product_type ' , self ::TYPE_CODE , $ product );
403
+ $ product ->addCustomOption ('info_buyRequest ' , serialize ($ buyRequest ->getData ()));
404
+
405
+ $ products [] = $ product ;
406
+ }
407
+
408
+ if (count ($ products )) {
409
+ return $ products ;
410
+ }
411
+
402
412
return __ ('Please specify the quantity of product(s). ' )->render ();
403
413
}
404
414
0 commit comments