Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TierPrice.php
Go to the documentation of this file.
1 <?php
8 
17 use Magento\Customer\Model\Group\RetrieverInterface as CustomerGroupRetrieverInterface;
18 
24 {
28  const PRICE_CODE = 'tier_price';
29 
34  protected $customerSession;
35 
39  protected $customerGroup;
40 
46  protected $rawPriceList;
47 
53  protected $priceList;
54 
58  protected $groupManagement;
59 
63  private $customerGroupRetriever;
64 
74  public function __construct(
75  Product $saleableItem,
76  $quantity,
81  CustomerGroupRetrieverInterface $customerGroupRetriever = null
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  }
95 
101  public function getValue()
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  }
123 
133  protected function isFirstPriceBetter($firstPrice, $secondPrice)
134  {
135  return $firstPrice < $secondPrice;
136  }
137 
141  public function getTierPriceCount()
142  {
143  return count($this->getTierPriceList());
144  }
145 
149  public function getTierPriceList()
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  }
165 
170  protected function filterTierPrices(array $priceList)
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  }
205 
209  protected function getBasePrice()
210  {
212  return $this->priceInfo->getPrice(BasePrice::PRICE_CODE)->getValue();
213  }
214 
224  {
225  $productPriceAmount = $this->priceInfo->getPrice(
227  )->getAmount();
228 
229  return round(
230  100 - ((100 / $productPriceAmount->getValue()) * $amount->getValue())
231  );
232  }
233 
238  protected function applyAdjustment($price)
239  {
240  return $this->calculator->getAmount($price, $this->product);
241  }
242 
251  protected function canApplyTierPrice(array $currentTierPrice, $prevPriceGroup, $prevQty)
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  }
278 
285  protected function getStoredTierPrices()
286  {
287  if (null === $this->rawPriceList) {
288  $this->rawPriceList = $this->product->getData(self::PRICE_CODE);
289  if (null === $this->rawPriceList || !is_array($this->rawPriceList)) {
291  $attribute = $this->product->getResource()->getAttribute(self::PRICE_CODE);
292  if ($attribute) {
293  $attribute->getBackend()->afterLoad($this->product);
294  $this->rawPriceList = $this->product->getData(self::PRICE_CODE);
295  }
296  }
297  if (null === $this->rawPriceList || !is_array($this->rawPriceList)) {
298  $this->rawPriceList = [];
299  }
300  if (!$this->isPercentageDiscount()) {
301  foreach ($this->rawPriceList as $index => $rawPrice) {
302  if (isset($rawPrice['price'])) {
303  $this->rawPriceList[$index]['price'] =
304  $this->priceCurrency->convertAndRound($rawPrice['price']);
305  }
306  if (isset($rawPrice['website_price'])) {
307  $this->rawPriceList[$index]['website_price'] =
308  $this->priceCurrency->convertAndRound($rawPrice['website_price']);
309  }
310  }
311  }
312  }
313  return $this->rawPriceList;
314  }
315 
319  public function isPercentageDiscount()
320  {
321  return false;
322  }
323 }
$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
getSavePercent(AmountInterface $amount)
Definition: TierPrice.php:223
__construct(Product $saleableItem, $quantity, CalculatorInterface $calculator, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, Session $customerSession, GroupManagementInterface $groupManagement, CustomerGroupRetrieverInterface $customerGroupRetriever=null)
Definition: TierPrice.php:74
isFirstPriceBetter($firstPrice, $secondPrice)
Definition: TierPrice.php:133
$index
Definition: list.phtml:44