calculator = $calculator; $this->selectionFactory = $bundleSelectionFactory; } /** * Get Options with attached Selections collection. * * @param SaleableInterface $bundleProduct * @return \Magento\Bundle\Model\ResourceModel\Option\Collection|array */ public function getOptions(SaleableInterface $bundleProduct) { /** @var \Magento\Bundle\Model\Product\Type $typeInstance */ $typeInstance = $bundleProduct->getTypeInstance(); $typeInstance->setStoreFilter($bundleProduct->getStoreId(), $bundleProduct); /** @var \Magento\Bundle\Model\ResourceModel\Option\Collection $optionCollection */ $optionCollection = $typeInstance->getOptionsCollection($bundleProduct); /** @var \Magento\Bundle\Model\ResourceModel\Selection\Collection $selectionCollection */ $selectionCollection = $typeInstance->getSelectionsCollection( $typeInstance->getOptionsIds($bundleProduct), $bundleProduct ); $priceOptions = $optionCollection->appendSelections($selectionCollection, true, false); return $priceOptions; } /** * Calculate maximal or minimal options value. * * @param SaleableInterface $bundleProduct * @param bool $searchMin * * @return float */ public function calculateOptions( SaleableInterface $bundleProduct, bool $searchMin = true ) : float { $priceList = []; /* @var \Magento\Bundle\Model\Option $option */ foreach ($this->getOptions($bundleProduct) as $option) { if ($searchMin && !$option->getRequired()) { continue; } /** @var \Magento\Bundle\Pricing\Price\BundleSelectionPrice $selectionPriceList */ $selectionPriceList = $this->calculator->createSelectionPriceList($option, $bundleProduct); $selectionPriceList = $this->calculator->processOptions($option, $selectionPriceList, $searchMin); $priceList = array_merge($priceList, $selectionPriceList); } $amount = $this->calculator->calculateBundleAmount(0., $bundleProduct, $priceList); return $amount->getValue(); } /** * Get selection amount. * * @param Product $bundleProduct * @param \Magento\Bundle\Model\Selection|Product $selection * @param bool $useRegularPrice * * @return AmountInterface */ public function getOptionSelectionAmount( Product $bundleProduct, $selection, bool $useRegularPrice = false ) : AmountInterface { $cacheKey = implode( '_', [ $bundleProduct->getId(), $selection->getOptionId(), $selection->getSelectionId(), $useRegularPrice ? 1 : 0, ] ); if (!isset($this->optionSelectionAmountCache[$cacheKey])) { $selectionPrice = $this->selectionFactory ->create( $bundleProduct, $selection, $selection->getSelectionQty(), ['useRegularPrice' => $useRegularPrice] ); $this->optionSelectionAmountCache[$cacheKey] = $selectionPrice->getAmount(); } return $this->optionSelectionAmountCache[$cacheKey]; } }