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

Public Member Functions

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

Protected Attributes

 $amountFactory
 

Detailed Description

Class Calculator

Definition at line 15 of file Calculator.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( AmountFactory  $amountFactory)
Parameters
AmountFactory$amountFactory

Definition at line 25 of file Calculator.php.

26  {
27  $this->amountFactory = $amountFactory;
28  }

Member Function Documentation

◆ getAmount()

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

Retrieve Amount object based on given float amount, product and exclude option. It is possible to pass "true" or adjustment code to exclude all or specific adjustment from an amount.

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

Implements CalculatorInterface.

Definition at line 41 of file Calculator.php.

42  {
43  $baseAmount = $fullAmount = $amount;
44  $previousAdjustments = 0;
45  $adjustments = [];
46  foreach ($saleableItem->getPriceInfo()->getAdjustments() as $adjustment) {
47  $code = $adjustment->getAdjustmentCode();
48  $toExclude = false;
49  if (!is_array($exclude)) {
50  if ($exclude === true || ($exclude !== null && $code === $exclude)) {
51  $toExclude = true;
52  }
53  } else {
54  if (in_array($code, $exclude)) {
55  $toExclude = true;
56  }
57  }
58  if ($adjustment->isIncludedInBasePrice()) {
59  $adjust = $adjustment->extractAdjustment($baseAmount, $saleableItem, $context);
60  $baseAmount -= $adjust;
61  $fullAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem, $context);
62  $adjust = $fullAmount - $baseAmount - $previousAdjustments;
63  if (!$toExclude) {
64  $adjustments[$code] = $adjust;
65  }
66  } elseif ($adjustment->isIncludedInDisplayPrice($saleableItem)) {
67  if ($toExclude) {
68  continue;
69  }
70  $newAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem, $context);
71  $adjust = $newAmount - $fullAmount;
72  $adjustments[$code] = $adjust;
73  $fullAmount = $newAmount;
74  $previousAdjustments += $adjust;
75  }
76  }
77 
78  return $this->amountFactory->create($fullAmount, $adjustments);
79  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$baseAmount
Definition: tax.phtml:46
$amount
Definition: order.php:14
$code
Definition: info.phtml:12

Field Documentation

◆ $amountFactory

$amountFactory
protected

Definition at line 20 of file Calculator.php.


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