7
7
8
8
use Magento \Bundle \Model \Option ;
9
9
use Magento \Catalog \Model \Product ;
10
+ use Magento \Framework \DataObject ;
10
11
11
12
/**
12
13
* Catalog bundle product info block
@@ -166,7 +167,7 @@ public function getJsonConfig()
166
167
167
168
$ defaultValues = [];
168
169
$ preConfiguredFlag = $ currentProduct ->hasPreconfiguredValues ();
169
- /** @var \Magento\Framework\ DataObject|null $preConfiguredValues */
170
+ /** @var DataObject|null $preConfiguredValues */
170
171
$ preConfiguredValues = $ preConfiguredFlag ? $ currentProduct ->getPreconfiguredValues () : null ;
171
172
172
173
$ position = 0 ;
@@ -185,12 +186,13 @@ public function getJsonConfig()
185
186
if ($ configValue ) {
186
187
$ defaultValues [$ optionId ] = $ configValue ;
187
188
}
189
+ $ options = $ this ->processOptions ($ optionId , $ options , $ preConfiguredValues );
188
190
}
189
191
$ position ++;
190
192
}
191
193
$ config = $ this ->getConfigData ($ currentProduct , $ options );
192
194
193
- $ configObj = new \ Magento \ Framework \ DataObject (
195
+ $ configObj = new DataObject (
194
196
[
195
197
'config ' => $ config ,
196
198
]
@@ -393,4 +395,30 @@ private function getConfigData(Product $product, array $options)
393
395
];
394
396
return $ config ;
395
397
}
398
+
399
+ /**
400
+ * Set preconfigured quantities and selections to options.
401
+ *
402
+ * @param string $optionId
403
+ * @param array $options
404
+ * @param DataObject $preConfiguredValues
405
+ * @return array
406
+ */
407
+ private function processOptions (string $ optionId , array $ options , DataObject $ preConfiguredValues )
408
+ {
409
+ $ preConfiguredQtys = $ preConfiguredValues ->getData ("bundle_option_qty/ $ {optionId}" ) ?? [];
410
+ $ selections = $ options [$ optionId ]['selections ' ];
411
+ array_walk ($ selections , function (&$ selection , $ selectionId ) use ($ preConfiguredQtys ) {
412
+ if (is_array ($ preConfiguredQtys ) && isset ($ preConfiguredQtys [$ selectionId ])) {
413
+ $ selection ['qty ' ] = $ preConfiguredQtys [$ selectionId ];
414
+ } else {
415
+ if ((int )$ preConfiguredQtys > 0 ) {
416
+ $ selection ['qty ' ] = $ preConfiguredQtys ;
417
+ }
418
+ }
419
+ });
420
+ $ options [$ optionId ]['selections ' ] = $ selections ;
421
+
422
+ return $ options ;
423
+ }
396
424
}
0 commit comments