Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BundleOptions.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
19 {
23  private $calculator;
24 
28  private $selectionFactory;
29 
33  private $optionSelectionAmountCache = [];
34 
39  public function __construct(
40  BundleCalculatorInterface $calculator,
41  BundleSelectionFactory $bundleSelectionFactory
42  ) {
43  $this->calculator = $calculator;
44  $this->selectionFactory = $bundleSelectionFactory;
45  }
46 
53  public function getOptions(SaleableInterface $bundleProduct)
54  {
56  $typeInstance = $bundleProduct->getTypeInstance();
57  $typeInstance->setStoreFilter($bundleProduct->getStoreId(), $bundleProduct);
58 
60  $optionCollection = $typeInstance->getOptionsCollection($bundleProduct);
61 
63  $selectionCollection = $typeInstance->getSelectionsCollection(
64  $typeInstance->getOptionsIds($bundleProduct),
66  );
67 
68  $priceOptions = $optionCollection->appendSelections($selectionCollection, true, false);
69 
70  return $priceOptions;
71  }
72 
81  public function calculateOptions(
82  SaleableInterface $bundleProduct,
83  bool $searchMin = true
84  ) : float {
85  $priceList = [];
86  /* @var \Magento\Bundle\Model\Option $option */
87  foreach ($this->getOptions($bundleProduct) as $option) {
88  if ($searchMin && !$option->getRequired()) {
89  continue;
90  }
92  $selectionPriceList = $this->calculator->createSelectionPriceList($option, $bundleProduct);
93  $selectionPriceList = $this->calculator->processOptions($option, $selectionPriceList, $searchMin);
94  $priceList = array_merge($priceList, $selectionPriceList);
95  }
96  $amount = $this->calculator->calculateBundleAmount(0., $bundleProduct, $priceList);
97 
98  return $amount->getValue();
99  }
100 
110  public function getOptionSelectionAmount(
112  $selection,
113  bool $useRegularPrice = false
114  ) : AmountInterface {
115  $cacheKey = implode(
116  '_',
117  [
118  $bundleProduct->getId(),
119  $selection->getOptionId(),
120  $selection->getSelectionId(),
121  $useRegularPrice ? 1 : 0,
122  ]
123  );
124 
125  if (!isset($this->optionSelectionAmountCache[$cacheKey])) {
126  $selectionPrice = $this->selectionFactory
127  ->create(
129  $selection,
130  $selection->getSelectionQty(),
131  ['useRegularPrice' => $useRegularPrice]
132  );
133  $this->optionSelectionAmountCache[$cacheKey] = $selectionPrice->getAmount();
134  }
135 
136  return $this->optionSelectionAmountCache[$cacheKey];
137  }
138 }
__construct(BundleCalculatorInterface $calculator, BundleSelectionFactory $bundleSelectionFactory)
getOptionSelectionAmount(Product $bundleProduct, $selection, bool $useRegularPrice=false)
$amount
Definition: order.php:14
$selectionCollection
$bundleProduct