77
88use Magento \Bundle \Model \Option ;
99use Magento \Catalog \Model \Product ;
10+ use Magento \Framework \DataObject ;
1011
1112/**
1213 * Catalog bundle product info block
@@ -170,7 +171,7 @@ public function getJsonConfig()
170171
171172 $ defaultValues = [];
172173 $ preConfiguredFlag = $ currentProduct ->hasPreconfiguredValues ();
173- /** @var \Magento\Framework\ DataObject|null $preConfiguredValues */
174+ /** @var DataObject|null $preConfiguredValues */
174175 $ preConfiguredValues = $ preConfiguredFlag ? $ currentProduct ->getPreconfiguredValues () : null ;
175176
176177 $ position = 0 ;
@@ -193,23 +194,13 @@ public function getJsonConfig()
193194 $ options [$ optionId ]['selections ' ][$ configValue ]['qty ' ] = $ configQty ;
194195 }
195196 }
196-
197- $ preConfiguredQtys = $ preConfiguredValues ->getData ("bundle_option_qty/ $ {optionId}" ) ?? [];
198- $ selections = $ options [$ optionId ]['selections ' ];
199- array_walk ($ selections , function (&$ selection , $ selectionId ) use ($ preConfiguredQtys ) {
200- if (is_array ($ preConfiguredQtys ) && isset ($ preConfiguredQtys [$ selectionId ])) {
201- $ selection ['qty ' ] = $ preConfiguredQtys [$ selectionId ];
202- } else if ((int )$ preConfiguredQtys > 0 ) {
203- $ selection ['qty ' ] = $ preConfiguredQtys ;
204- }
205- });
206- $ options [$ optionId ]['selections ' ] = $ selections ;
197+ $ options = $ this ->processOptions ($ optionId , $ options , $ preConfiguredValues );
207198 }
208199 $ position ++;
209200 }
210201 $ config = $ this ->getConfigData ($ currentProduct , $ options );
211202
212- $ configObj = new \ Magento \ Framework \ DataObject (
203+ $ configObj = new DataObject (
213204 [
214205 'config ' => $ config ,
215206 ]
@@ -414,4 +405,30 @@ private function getConfigData(Product $product, array $options)
414405 ];
415406 return $ config ;
416407 }
408+
409+ /**
410+ * Set preconfigured quantities and selections to options.
411+ *
412+ * @param string $optionId
413+ * @param array $options
414+ * @param DataObject $preConfiguredValues
415+ * @return array
416+ */
417+ private function processOptions (string $ optionId , array $ options , DataObject $ preConfiguredValues )
418+ {
419+ $ preConfiguredQtys = $ preConfiguredValues ->getData ("bundle_option_qty/ $ {optionId}" ) ?? [];
420+ $ selections = $ options [$ optionId ]['selections ' ];
421+ array_walk ($ selections , function (&$ selection , $ selectionId ) use ($ preConfiguredQtys ) {
422+ if (is_array ($ preConfiguredQtys ) && isset ($ preConfiguredQtys [$ selectionId ])) {
423+ $ selection ['qty ' ] = $ preConfiguredQtys [$ selectionId ];
424+ } else {
425+ if ((int )$ preConfiguredQtys > 0 ) {
426+ $ selection ['qty ' ] = $ preConfiguredQtys ;
427+ }
428+ }
429+ });
430+ $ options [$ optionId ]['selections ' ] = $ selections ;
431+
432+ return $ options ;
433+ }
417434}
0 commit comments