77
88use Magento \Bundle \Model \Option ;
99use Magento \Catalog \Model \Product ;
10+ use Magento \Framework \DataObject ;
1011
1112/**
1213 * Catalog bundle product info block
@@ -166,7 +167,7 @@ public function getJsonConfig()
166167
167168 $ defaultValues = [];
168169 $ preConfiguredFlag = $ currentProduct ->hasPreconfiguredValues ();
169- /** @var \Magento\Framework\ DataObject|null $preConfiguredValues */
170+ /** @var DataObject|null $preConfiguredValues */
170171 $ preConfiguredValues = $ preConfiguredFlag ? $ currentProduct ->getPreconfiguredValues () : null ;
171172
172173 $ position = 0 ;
@@ -185,23 +186,13 @@ public function getJsonConfig()
185186 if ($ configValue ) {
186187 $ defaultValues [$ optionId ] = $ configValue ;
187188 }
188-
189- $ preConfiguredQtys = $ preConfiguredValues ->getData ("bundle_option_qty/ $ {optionId}" ) ?? [];
190- $ selections = $ options [$ optionId ]['selections ' ];
191- array_walk ($ selections , function (&$ selection , $ selectionId ) use ($ preConfiguredQtys ) {
192- if (is_array ($ preConfiguredQtys ) && isset ($ preConfiguredQtys [$ selectionId ])) {
193- $ selection ['qty ' ] = $ preConfiguredQtys [$ selectionId ];
194- } else if ((int )$ preConfiguredQtys > 0 ) {
195- $ selection ['qty ' ] = $ preConfiguredQtys ;
196- }
197- });
198- $ options [$ optionId ]['selections ' ] = $ selections ;
189+ $ options = $ this ->processOptions ($ optionId , $ options , $ preConfiguredValues );
199190 }
200191 $ position ++;
201192 }
202193 $ config = $ this ->getConfigData ($ currentProduct , $ options );
203194
204- $ configObj = new \ Magento \ Framework \ DataObject (
195+ $ configObj = new DataObject (
205196 [
206197 'config ' => $ config ,
207198 ]
@@ -404,4 +395,30 @@ private function getConfigData(Product $product, array $options)
404395 ];
405396 return $ config ;
406397 }
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+ }
407424}
0 commit comments