Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Calculator Class Reference
Inheritance diagram for Calculator:
BundleCalculatorInterface CalculatorInterface

Public Member Functions

 __construct (CalculatorBase $calculator, AmountFactory $amountFactory, BundleSelectionFactory $bundleSelectionFactory, TaxHelper $taxHelper, PriceCurrencyInterface $priceCurrency, SelectionPriceListProviderInterface $selectionPriceListProvider=null)
 
 getAmount ($amount, SaleableInterface $saleableItem, $exclude=null, $context=[])
 
 getMinRegularAmount ($amount, Product $saleableItem, $exclude=null)
 
 getMaxAmount ($amount, Product $saleableItem, $exclude=null)
 
 getMaxRegularAmount ($amount, Product $saleableItem, $exclude=null)
 
 getOptionsAmount (Product $saleableItem, $exclude=null, $searchMin=true, $baseAmount=0., $useRegularPrice=false)
 
 getAmountWithoutOption ($amount, Product $saleableItem)
 
 calculateBundleAmount ($basePriceValue, $bundleProduct, $selectionPriceList, $exclude=null)
 
 createSelectionPriceList ($option, $bundleProduct, $useRegularPrice=false)
 
 processOptions ($option, $selectionPriceList, $searchMin=true)
 
- Public Member Functions inherited from BundleCalculatorInterface
 getOptionsAmount (Product $saleableItem, $exclude=null, $searchMin=true, $bundleProductAmount=null)
 

Protected Member Functions

 getSelectionAmounts (Product $bundleProduct, $searchMin, $useRegularPrice=false)
 
 canSkipOption ($option, $canSkipRequiredOption)
 
 hasRequiredOption ($bundleProduct)
 

Protected Attributes

 $calculator
 
 $amountFactory
 
 $selectionFactory
 
 $taxHelper
 
 $priceCurrency
 

Detailed Description

Bundle price calculator @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 24 of file Calculator.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( CalculatorBase  $calculator,
AmountFactory  $amountFactory,
BundleSelectionFactory  $bundleSelectionFactory,
TaxHelper  $taxHelper,
PriceCurrencyInterface  $priceCurrency,
SelectionPriceListProviderInterface  $selectionPriceListProvider = null 
)
Parameters
CalculatorBase$calculator
AmountFactory$amountFactory
BundleSelectionFactory$bundleSelectionFactory
TaxHelper$taxHelper
PriceCurrencyInterface$priceCurrency
SelectionPriceListProviderInterface | null$selectionPriceListProvider

Definition at line 71 of file Calculator.php.

78  {
79  $this->calculator = $calculator;
80  $this->amountFactory = $amountFactory;
81  $this->selectionFactory = $bundleSelectionFactory;
82  $this->taxHelper = $taxHelper;
83  $this->priceCurrency = $priceCurrency;
84  $this->selectionPriceListProvider = $selectionPriceListProvider;
85  }

Member Function Documentation

◆ calculateBundleAmount()

calculateBundleAmount (   $basePriceValue,
  $bundleProduct,
  $selectionPriceList,
  $exclude = null 
)

Calculate amount for bundle product with all selection prices

Parameters
float$basePriceValue
Product$bundleProduct

Implements BundleCalculatorInterface.

Definition at line 270 of file Calculator.php.

271  {
272  if ($bundleProduct->getPriceType() == Price::PRICE_TYPE_FIXED) {
273  return $this->calculateFixedBundleAmount($basePriceValue, $bundleProduct, $selectionPriceList, $exclude);
274  }
275  return $this->calculateDynamicBundleAmount($basePriceValue, $bundleProduct, $selectionPriceList, $exclude);
276  }
$bundleProduct

◆ canSkipOption()

canSkipOption (   $option,
  $canSkipRequiredOption 
)
protected

Check this option if it should be skipped

Parameters
\Magento\Bundle\Model\Option$option
bool$canSkipRequiredOption
Returns
bool
Deprecated:
100.2.0

Definition at line 222 of file Calculator.php.

223  {
224  return !$option->getSelections() || ($canSkipRequiredOption && !$option->getRequired());
225  }

◆ createSelectionPriceList()

createSelectionPriceList (   $option,
  $bundleProduct,
  $useRegularPrice = false 
)

Create selection price list for the retrieved options

Parameters
\Magento\Bundle\Model\Option$option
Product$bundleProduct
bool$useRegularPrice
Returns
\Magento\Bundle\Pricing\Price\BundleSelectionPrice[]

Implements BundleCalculatorInterface.

Definition at line 372 of file Calculator.php.

373  {
374  $priceList = [];
375  $selections = $option->getSelections();
376  if ($selections === null) {
377  return $priceList;
378  }
379  /* @var $selection \Magento\Bundle\Model\Selection|\Magento\Catalog\Model\Product */
380  foreach ($selections as $selection) {
381  if (!$selection->isSalable()) {
382  // @todo CatalogInventory Show out of stock Products
383  continue;
384  }
385  $priceList[] = $this->selectionFactory->create(
387  $selection,
388  $selection->getSelectionQty(),
389  [
390  'useRegularPrice' => $useRegularPrice,
391  ]
392  );
393  }
394  return $priceList;
395  }
$bundleProduct

◆ getAmount()

getAmount (   $amount,
SaleableInterface  $saleableItem,
  $exclude = null,
  $context = [] 
)

Get amount for current product which is included price of existing options with minimal price

Parameters
float | string$amount
SaleableInterface$saleableItem
null | bool | string | array$exclude
null | array$context
Returns
\Magento\Framework\Pricing\Amount\AmountInterface @SuppressWarnings(PHPMD.UnusedFormalParameter)

Implements CalculatorInterface.

Definition at line 97 of file Calculator.php.

98  {
99  return $this->getOptionsAmount($saleableItem, $exclude, true, $amount);
100  }
$amount
Definition: order.php:14
getOptionsAmount(Product $saleableItem, $exclude=null, $searchMin=true, $baseAmount=0., $useRegularPrice=false)
Definition: Calculator.php:151

◆ getAmountWithoutOption()

getAmountWithoutOption (   $amount,
Product  $saleableItem 
)

Get base amount without option

Parameters
float$amount
Product$saleableItem
Returns
\Magento\Framework\Pricing\Amount\AmountInterface|void

Implements BundleCalculatorInterface.

Definition at line 178 of file Calculator.php.

179  {
180  return $this->calculateBundleAmount(
181  $amount,
182  $saleableItem,
183  []
184  );
185  }
$amount
Definition: order.php:14
calculateBundleAmount($basePriceValue, $bundleProduct, $selectionPriceList, $exclude=null)
Definition: Calculator.php:270

◆ getMaxAmount()

getMaxAmount (   $amount,
Product  $saleableItem,
  $exclude = null 
)

Get amount for current product which is included price of existing options with maximal price

Parameters
float$amount
Product$saleableItem
null | bool | string | array$exclude
Returns
\Magento\Framework\Pricing\Amount\AmountInterface

Implements BundleCalculatorInterface.

Definition at line 123 of file Calculator.php.

124  {
125  return $this->getOptionsAmount($saleableItem, $exclude, false, $amount);
126  }
$amount
Definition: order.php:14
getOptionsAmount(Product $saleableItem, $exclude=null, $searchMin=true, $baseAmount=0., $useRegularPrice=false)
Definition: Calculator.php:151

◆ getMaxRegularAmount()

getMaxRegularAmount (   $amount,
Product  $saleableItem,
  $exclude = null 
)

Get amount for current product which is included price of existing options with maximal price

Parameters
float$amount
Product$saleableItem
null | bool | string | array$exclude
Returns
\Magento\Framework\Pricing\Amount\AmountInterface

Implements BundleCalculatorInterface.

Definition at line 136 of file Calculator.php.

137  {
138  return $this->getOptionsAmount($saleableItem, $exclude, false, $amount, true);
139  }
$amount
Definition: order.php:14
getOptionsAmount(Product $saleableItem, $exclude=null, $searchMin=true, $baseAmount=0., $useRegularPrice=false)
Definition: Calculator.php:151

◆ getMinRegularAmount()

getMinRegularAmount (   $amount,
Product  $saleableItem,
  $exclude = null 
)

Get amount for current product which is included price of existing options with maximal price

Parameters
float$amount
Product$saleableItem
null | bool | string | array$exclude
Returns
\Magento\Framework\Pricing\Amount\AmountInterface

Implements BundleCalculatorInterface.

Definition at line 110 of file Calculator.php.

111  {
112  return $this->getOptionsAmount($saleableItem, $exclude, true, $amount, true);
113  }
$amount
Definition: order.php:14
getOptionsAmount(Product $saleableItem, $exclude=null, $searchMin=true, $baseAmount=0., $useRegularPrice=false)
Definition: Calculator.php:151

◆ getOptionsAmount()

getOptionsAmount ( Product  $saleableItem,
  $exclude = null,
  $searchMin = true,
  $baseAmount = 0.,
  $useRegularPrice = false 
)

Option amount calculation for bundle product

Parameters
Product$saleableItem
null | bool | string | array$exclude
bool$searchMin
float$baseAmount
bool$useRegularPrice
Returns
\Magento\Framework\Pricing\Amount\AmountInterface

Definition at line 151 of file Calculator.php.

157  {
158  $cacheKey = implode('-', [$saleableItem->getId(), $exclude, $searchMin, $baseAmount, $useRegularPrice]);
159  if (!isset($this->optionAmount[$cacheKey])) {
160  $this->optionAmount[$cacheKey] = $this->calculateBundleAmount(
161  $baseAmount,
162  $saleableItem,
163  $this->getSelectionAmounts($saleableItem, $searchMin, $useRegularPrice),
164  $exclude
165  );
166  }
167 
168  return $this->optionAmount[$cacheKey];
169  }
getSelectionAmounts(Product $bundleProduct, $searchMin, $useRegularPrice=false)
Definition: Calculator.php:195
$baseAmount
Definition: tax.phtml:46
calculateBundleAmount($basePriceValue, $bundleProduct, $selectionPriceList, $exclude=null)
Definition: Calculator.php:270

◆ getSelectionAmounts()

getSelectionAmounts ( Product  $bundleProduct,
  $searchMin,
  $useRegularPrice = false 
)
protected

Filter all options for bundle product

Parameters
Product$bundleProduct
bool$searchMin
bool$useRegularPrice
Returns
array

Definition at line 195 of file Calculator.php.

196  {
197  return $this->getSelectionPriceListProvider()->getPriceList($bundleProduct, $searchMin, $useRegularPrice);
198  }
$bundleProduct

◆ hasRequiredOption()

hasRequiredOption (   $bundleProduct)
protected

Check the bundle product for availability of required options

Parameters
Product$bundleProduct
Returns
bool
Deprecated:
100.2.0

Definition at line 234 of file Calculator.php.

235  {
236  $options = array_filter(
237  $this->getBundleOptions($bundleProduct),
238  function ($item) {
239  return $item->getRequired();
240  }
241  );
242  return !empty($options);
243  }
$bundleProduct

◆ processOptions()

processOptions (   $option,
  $selectionPriceList,
  $searchMin = true 
)

Find minimal or maximal price for existing options

Parameters
\Magento\Bundle\Model\Option$option

Implements BundleCalculatorInterface.

Definition at line 406 of file Calculator.php.

407  {
408  $result = [];
409  foreach ($selectionPriceList as $current) {
410  $qty = $current->getQuantity();
411  $currentValue = $current->getAmount()->getValue() * $qty;
412  if (empty($result)) {
413  $result = [$current];
414  } else {
415  $lastSelectionPrice = end($result);
416  $lastValue = $lastSelectionPrice->getAmount()->getValue() * $lastSelectionPrice->getQuantity();
417  if ($searchMin && $lastValue > $currentValue) {
418  $result = [$current];
419  } elseif (!$searchMin && $option->isMultiSelection()) {
420  $result[] = $current;
421  } elseif (!$searchMin
422  && !$option->isMultiSelection()
423  && $lastValue < $currentValue
424  ) {
425  $result = [$current];
426  }
427  }
428  }
429  return $result;
430  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

Field Documentation

◆ $amountFactory

$amountFactory
protected

Definition at line 34 of file Calculator.php.

◆ $calculator

$calculator
protected

Definition at line 29 of file Calculator.php.

◆ $priceCurrency

$priceCurrency
protected

Definition at line 51 of file Calculator.php.

◆ $selectionFactory

$selectionFactory
protected

Definition at line 39 of file Calculator.php.

◆ $taxHelper

$taxHelper
protected

Definition at line 46 of file Calculator.php.


The documentation for this class was generated from the following file: