Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
TierPrice Class Reference
Inheritance diagram for TierPrice:
AbstractPrice TierPriceInterface BasePriceProviderInterface PriceInterface TierPrice

Public Member Functions

 __construct (Product $saleableItem, $quantity, CalculatorInterface $calculator, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, Session $customerSession, GroupManagementInterface $groupManagement, CustomerGroupRetrieverInterface $customerGroupRetriever=null)
 
 getValue ()
 
 getTierPriceCount ()
 
 getTierPriceList ()
 
 getSavePercent (AmountInterface $amount)
 
 isPercentageDiscount ()
 
- Public Member Functions inherited from AbstractPrice
 __construct (SaleableInterface $saleableItem, $quantity, CalculatorInterface $calculator, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency)
 
 getValue ()
 
 getAmount ()
 
 getCustomAmount ($amount=null, $exclude=null, $context=[])
 
 getPriceCode ()
 
 getProduct ()
 
 getQuantity ()
 

Data Fields

const PRICE_CODE = 'tier_price'
 
- Data Fields inherited from AbstractPrice
const PRICE_CODE = 'abstract_price'
 

Protected Member Functions

 isFirstPriceBetter ($firstPrice, $secondPrice)
 
 filterTierPrices (array $priceList)
 
 applyAdjustment ($price)
 
 canApplyTierPrice (array $currentTierPrice, $prevPriceGroup, $prevQty)
 

Protected Attributes

 $customerSession
 
 $customerGroup
 
 $rawPriceList
 
 $priceList
 
 $groupManagement
 
- Protected Attributes inherited from AbstractPrice
 $amount
 
 $calculator
 
 $product
 
 $priceType
 
 $quantity
 
 $priceInfo
 
 $value
 
 $priceCurrency
 

Detailed Description

@api

Since
100.0.2

Definition at line 23 of file TierPrice.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Product  $saleableItem,
  $quantity,
CalculatorInterface  $calculator,
\Magento\Framework\Pricing\PriceCurrencyInterface  $priceCurrency,
Session  $customerSession,
GroupManagementInterface  $groupManagement,
CustomerGroupRetrieverInterface  $customerGroupRetriever = null 
)
Parameters
Product$saleableItem
float$quantity
CalculatorInterface$calculator
\Magento\Framework\Pricing\PriceCurrencyInterface$priceCurrency
Session$customerSession
GroupManagementInterface$groupManagement
CustomerGroupRetrieverInterface | null$customerGroupRetriever

Definition at line 74 of file TierPrice.php.

82  {
83  $quantity = (float)$quantity ? $quantity : 1;
84  parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
85  $this->customerSession = $customerSession;
86  $this->groupManagement = $groupManagement;
87  $this->customerGroupRetriever = $customerGroupRetriever
88  ?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomerGroupRetrieverInterface::class);
89  if ($saleableItem->hasCustomerGroupId()) {
90  $this->customerGroup = (int) $saleableItem->getCustomerGroupId();
91  } else {
92  $this->customerGroup = (int) $this->customerGroupRetriever->getCustomerGroupId();
93  }
94  }

Member Function Documentation

◆ applyAdjustment()

applyAdjustment (   $price)
protected
Parameters
float | string$price
Returns
\Magento\Framework\Pricing\Amount\AmountInterface

Definition at line 238 of file TierPrice.php.

239  {
240  return $this->calculator->getAmount($price, $this->product);
241  }
$price

◆ canApplyTierPrice()

canApplyTierPrice ( array  $currentTierPrice,
  $prevPriceGroup,
  $prevQty 
)
protected

Can apply tier price

Parameters
array$currentTierPrice
int$prevPriceGroup
float | string$prevQty
Returns
bool

Definition at line 251 of file TierPrice.php.

252  {
253  $custGroupAllId = (int)$this->groupManagement->getAllCustomersGroup()->getId();
254  // Tier price can be applied, if:
255  // tier price is for current customer group or is for all groups
256  if ((int)$currentTierPrice['cust_group'] !== $this->customerGroup
257  && (int)$currentTierPrice['cust_group'] !== $custGroupAllId
258  ) {
259  return false;
260  }
261  // and tier qty is lower than product qty
262  if ($this->quantity < $currentTierPrice['price_qty']) {
263  return false;
264  }
265  // and tier qty is bigger than previous qty
266  if ($currentTierPrice['price_qty'] < $prevQty) {
267  return false;
268  }
269  // and found tier qty is same as previous tier qty, but current tier group isn't ALL_GROUPS
270  if ($currentTierPrice['price_qty'] == $prevQty
271  && $prevPriceGroup !== $custGroupAllId
272  && $currentTierPrice['cust_group'] === $custGroupAllId
273  ) {
274  return false;
275  }
276  return true;
277  }

◆ filterTierPrices()

filterTierPrices ( array  $priceList)
protected
Parameters
array$priceList
Returns
array

Definition at line 170 of file TierPrice.php.

171  {
172  $qtyCache = [];
173  $allCustomersGroupId = $this->groupManagement->getAllCustomersGroup()->getId();
174  foreach ($priceList as $priceKey => &$price) {
175  if ($price['price'] >= $this->priceInfo->getPrice(FinalPrice::PRICE_CODE)->getValue()) {
176  unset($priceList[$priceKey]);
177  continue;
178  }
179 
180  if (isset($price['price_qty']) && $price['price_qty'] == 1) {
181  unset($priceList[$priceKey]);
182  continue;
183  }
184  /* filter price by customer group */
185  if ($price['cust_group'] != $this->customerGroup &&
186  $price['cust_group'] != $allCustomersGroupId) {
187  unset($priceList[$priceKey]);
188  continue;
189  }
190  /* select a lower price for each quantity */
191  if (isset($qtyCache[$price['price_qty']])) {
192  $priceQty = $qtyCache[$price['price_qty']];
193  if ($this->isFirstPriceBetter($price['website_price'], $priceList[$priceQty]['website_price'])) {
194  unset($priceList[$priceQty]);
195  $qtyCache[$price['price_qty']] = $priceKey;
196  } else {
197  unset($priceList[$priceKey]);
198  }
199  } else {
200  $qtyCache[$price['price_qty']] = $priceKey;
201  }
202  }
203  return array_values($priceList);
204  }
$price
isFirstPriceBetter($firstPrice, $secondPrice)
Definition: TierPrice.php:133

◆ getSavePercent()

getSavePercent ( AmountInterface  $amount)

Calculates savings percentage according to the given tier price amount and related product price amount.

Parameters
AmountInterface$amount
Returns
float

Definition at line 223 of file TierPrice.php.

224  {
225  $productPriceAmount = $this->priceInfo->getPrice(
227  )->getAmount();
228 
229  return round(
230  100 - ((100 / $productPriceAmount->getValue()) * $amount->getValue())
231  );
232  }

◆ getTierPriceCount()

getTierPriceCount ( )
Returns
int

Implements TierPriceInterface.

Definition at line 141 of file TierPrice.php.

142  {
143  return count($this->getTierPriceList());
144  }

◆ getTierPriceList()

getTierPriceList ( )
Returns
array

Implements TierPriceInterface.

Definition at line 149 of file TierPrice.php.

150  {
151  if (null === $this->priceList) {
152  $priceList = $this->getStoredTierPrices();
153  $this->priceList = $this->filterTierPrices($priceList);
154  array_walk(
155  $this->priceList,
156  function (&$priceData) {
157  /* convert string value to float */
158  $priceData['price_qty'] = $priceData['price_qty'] * 1;
159  $priceData['price'] = $this->applyAdjustment($priceData['price']);
160  }
161  );
162  }
163  return $this->priceList;
164  }

◆ getValue()

getValue ( )

Get price value

Returns
bool|float

Implements PriceInterface.

Definition at line 101 of file TierPrice.php.

102  {
103  if (null === $this->value) {
104  $prices = $this->getStoredTierPrices();
106  $this->value = $prevPrice = $tierPrice = false;
107  $priceGroup = $this->groupManagement->getAllCustomersGroup()->getId();
108 
109  foreach ($prices as $price) {
110  if (!$this->canApplyTierPrice($price, $priceGroup, $prevQty)) {
111  continue;
112  }
113  if (false === $prevPrice || $this->isFirstPriceBetter($price['website_price'], $prevPrice)) {
114  $tierPrice = $prevPrice = $price['website_price'];
115  $prevQty = $price['price_qty'];
116  $priceGroup = $price['cust_group'];
117  $this->value = (float)$tierPrice;
118  }
119  }
120  }
121  return $this->value;
122  }
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9
canApplyTierPrice(array $currentTierPrice, $prevPriceGroup, $prevQty)
Definition: TierPrice.php:251
$price
isFirstPriceBetter($firstPrice, $secondPrice)
Definition: TierPrice.php:133

◆ isFirstPriceBetter()

isFirstPriceBetter (   $firstPrice,
  $secondPrice 
)
protected

Returns true if first price is better

Method filters tiers price values, lower tier price value is better

Parameters
float$firstPrice
float$secondPrice
Returns
bool

Definition at line 133 of file TierPrice.php.

134  {
135  return $firstPrice < $secondPrice;
136  }

◆ isPercentageDiscount()

isPercentageDiscount ( )
Returns
bool

Implements TierPriceInterface.

Definition at line 319 of file TierPrice.php.

320  {
321  return false;
322  }

Field Documentation

◆ $customerGroup

$customerGroup
protected

Definition at line 39 of file TierPrice.php.

◆ $customerSession

$customerSession
protected

Definition at line 34 of file TierPrice.php.

◆ $groupManagement

$groupManagement
protected

Definition at line 58 of file TierPrice.php.

◆ $priceList

$priceList
protected

Definition at line 53 of file TierPrice.php.

◆ $rawPriceList

$rawPriceList
protected

Definition at line 46 of file TierPrice.php.

◆ PRICE_CODE

const PRICE_CODE = 'tier_price'

Price type tier

Definition at line 28 of file TierPrice.php.


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