@@ -55,25 +55,33 @@ class Calculator implements BundleCalculatorInterface
55
55
*/
56
56
private $ optionAmount = [];
57
57
58
+ /**
59
+ * @var SelectionPriceListProviderInterface
60
+ */
61
+ private $ selectionPriceListProvider ;
62
+
58
63
/**
59
64
* @param CalculatorBase $calculator
60
65
* @param AmountFactory $amountFactory
61
66
* @param BundleSelectionFactory $bundleSelectionFactory
62
67
* @param TaxHelper $taxHelper
63
68
* @param PriceCurrencyInterface $priceCurrency
69
+ * @param SelectionPriceListProviderInterface|null $selectionPriceListProvider
64
70
*/
65
71
public function __construct (
66
72
CalculatorBase $ calculator ,
67
73
AmountFactory $ amountFactory ,
68
74
BundleSelectionFactory $ bundleSelectionFactory ,
69
75
TaxHelper $ taxHelper ,
70
- PriceCurrencyInterface $ priceCurrency
76
+ PriceCurrencyInterface $ priceCurrency ,
77
+ SelectionPriceListProviderInterface $ selectionPriceListProvider = null
71
78
) {
72
79
$ this ->calculator = $ calculator ;
73
80
$ this ->amountFactory = $ amountFactory ;
74
81
$ this ->selectionFactory = $ bundleSelectionFactory ;
75
82
$ this ->taxHelper = $ taxHelper ;
76
83
$ this ->priceCurrency = $ priceCurrency ;
84
+ $ this ->selectionPriceListProvider = $ selectionPriceListProvider ;
77
85
}
78
86
79
87
/**
@@ -183,90 +191,24 @@ public function getAmountWithoutOption($amount, Product $saleableItem)
183
191
* @param bool $searchMin
184
192
* @param bool $useRegularPrice
185
193
* @return array
186
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
187
- * @SuppressWarnings(PHPMD.NPathComplexity)
188
194
*/
189
195
protected function getSelectionAmounts (Product $ bundleProduct , $ searchMin , $ useRegularPrice = false )
190
196
{
191
- // Flag shows - is it necessary to find minimal option amount in case if all options are not required
192
- $ shouldFindMinOption = false ;
193
- if ($ searchMin
194
- && $ bundleProduct ->getPriceType () == Price::PRICE_TYPE_DYNAMIC
195
- && !$ this ->hasRequiredOption ($ bundleProduct )
196
- ) {
197
- $ shouldFindMinOption = true ;
198
- }
199
- $ canSkipRequiredOptions = $ searchMin && !$ shouldFindMinOption ;
200
-
201
- /** @var \Magento\Bundle\Model\Product\Type $typeInstance */
202
- $ typeInstance = $ bundleProduct ->getTypeInstance ();
203
- $ priceList = [];
204
-
205
- foreach ($ this ->getBundleOptions ($ bundleProduct ) as $ option ) {
206
- /** @var \Magento\Bundle\Model\Option $option */
207
- if ($ this ->canSkipOption ($ option , $ canSkipRequiredOptions )) {
208
- continue ;
209
- }
210
-
211
- $ selectionsCollection = $ typeInstance ->getSelectionsCollection (
212
- [(int )$ option ->getOptionId ()],
213
- $ bundleProduct
214
- );
215
- $ selectionsCollection ->removeAttributeToSelect ();
216
- $ selectionsCollection ->addQuantityFilter ();
217
-
218
- if (!$ searchMin && $ option ->isMultiSelection ()) {
219
- $ selectionsCollection ->addPriceData ();
220
-
221
- foreach ($ selectionsCollection as $ selection ) {
222
- $ priceList [] = $ this ->selectionFactory ->create (
223
- $ bundleProduct ,
224
- $ selection ,
225
- $ selection ->getSelectionQty (),
226
- [
227
- 'useRegularPrice ' => $ useRegularPrice ,
228
- ]
229
- );
230
- }
231
- } else {
232
- $ selectionsCollection ->addPriceFilter ($ bundleProduct , $ searchMin , $ useRegularPrice );
233
- $ selectionsCollection ->setPage (0 , 1 );
234
- if (!$ useRegularPrice ) {
235
- $ selectionsCollection ->addAttributeToSelect ('special_price ' );
236
- $ selectionsCollection ->addAttributeToSelect ('special_price_from ' );
237
- $ selectionsCollection ->addAttributeToSelect ('special_price_to ' );
238
- $ selectionsCollection ->addAttributeToSelect ('tax_class_id ' );
239
- }
240
-
241
- $ selection = $ selectionsCollection ->getFirstItem ();
242
-
243
- if (!$ selection ->isEmpty ()) {
244
- $ priceList [] = $ this ->selectionFactory ->create (
245
- $ bundleProduct ,
246
- $ selection ,
247
- $ selection ->getSelectionQty (),
248
- [
249
- 'useRegularPrice ' => $ useRegularPrice ,
250
- ]
251
- );
252
- }
253
- }
254
- }
197
+ return $ this ->getSelectionPriceListProvider ()->getPriceList ($ bundleProduct , $ searchMin , $ useRegularPrice );
198
+ }
255
199
256
- if ($ shouldFindMinOption ) {
257
- $ minPrice = null ;
258
- $ priceSelection = null ;
259
- foreach ($ priceList as $ price ) {
260
- $ minPriceTmp = $ price ->getAmount ()->getValue () * $ price ->getQuantity ();
261
- if (!$ minPrice || $ minPriceTmp < $ minPrice ) {
262
- $ minPrice = $ minPriceTmp ;
263
- $ priceSelection = $ price ;
264
- }
265
- }
266
- $ priceList = $ priceSelection ? [$ priceSelection ] : [];
200
+ /**
201
+ * @return SelectionPriceListProviderInterface
202
+ * @deprecated
203
+ */
204
+ private function getSelectionPriceListProvider ()
205
+ {
206
+ if (null === $ this ->selectionPriceListProvider ) {
207
+ $ this ->selectionPriceListProvider = \Magento \Framework \App \ObjectManager::getInstance ()
208
+ ->get (SelectionPriceListProviderInterface::class);
267
209
}
268
210
269
- return $ priceList ;
211
+ return $ this -> selectionPriceListProvider ;
270
212
}
271
213
272
214
/**
@@ -275,35 +217,45 @@ protected function getSelectionAmounts(Product $bundleProduct, $searchMin, $useR
275
217
* @param \Magento\Bundle\Model\Option $option
276
218
* @param bool $canSkipRequiredOption
277
219
* @return bool
220
+ * @deprecated
278
221
*/
279
222
protected function canSkipOption ($ option , $ canSkipRequiredOption )
280
223
{
281
- return $ canSkipRequiredOption && !$ option ->getRequired ();
224
+ return ! $ option -> getSelections () || ( $ canSkipRequiredOption && !$ option ->getRequired () );
282
225
}
283
226
284
227
/**
285
228
* Check the bundle product for availability of required options
286
229
*
287
230
* @param Product $bundleProduct
288
231
* @return bool
232
+ * @deprecated
289
233
*/
290
234
protected function hasRequiredOption ($ bundleProduct )
291
235
{
292
- $ collection = clone $ this ->getBundleOptions ($ bundleProduct );
293
- $ collection ->clear ();
294
-
295
- return $ collection ->addFilter (\Magento \Bundle \Model \Option::KEY_REQUIRED , 1 )->getSize () > 0 ;
236
+ $ options = array_filter (
237
+ $ this ->getBundleOptions ($ bundleProduct ),
238
+ function ($ item ) {
239
+ return $ item ->getRequired ();
240
+ }
241
+ );
242
+ return !empty ($ options );
296
243
}
297
244
298
245
/**
299
246
* Get bundle options
300
247
*
301
248
* @param Product $saleableItem
302
249
* @return \Magento\Bundle\Model\ResourceModel\Option\Collection
250
+ * @deprecated
303
251
*/
304
252
protected function getBundleOptions (Product $ saleableItem )
305
253
{
306
- return $ saleableItem ->getTypeInstance ()->getOptionsCollection ($ saleableItem );
254
+ /** @var \Magento\Bundle\Pricing\Price\BundleOptionPrice $bundlePrice */
255
+ $ bundlePrice = $ saleableItem ->getPriceInfo ()->getPrice (
256
+ \Magento \Bundle \Pricing \Price \BundleOptionPrice::PRICE_CODE
257
+ );
258
+ return $ bundlePrice ->getOptions ();
307
259
}
308
260
309
261
/**
0 commit comments