TierPrice.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Pricing\Price;
  7. use Magento\Catalog\Pricing\Price\RegularPrice;
  8. use Magento\Framework\Pricing\Amount\AmountInterface;
  9. use Magento\Framework\Pricing\PriceInfoInterface;
  10. /**
  11. * Bundle tier prices model
  12. */
  13. class TierPrice extends \Magento\Catalog\Pricing\Price\TierPrice implements DiscountProviderInterface
  14. {
  15. /**
  16. * @var bool
  17. */
  18. protected $filterByBasePrice = false;
  19. /**
  20. * @var float|false
  21. */
  22. protected $percent;
  23. /**
  24. * Returns percent discount
  25. *
  26. * @return bool|float
  27. */
  28. public function getDiscountPercent()
  29. {
  30. if ($this->percent === null) {
  31. $prices = $this->getStoredTierPrices();
  32. $prevQty = PriceInfoInterface::PRODUCT_QUANTITY_DEFAULT;
  33. $this->value = $prevPrice = false;
  34. $priceGroup = $this->groupManagement->getAllCustomersGroup()->getId();
  35. foreach ($prices as $price) {
  36. if (!$this->canApplyTierPrice($price, $priceGroup, $prevQty)
  37. || !isset($price['percentage_value'])
  38. || !is_numeric($price['percentage_value'])
  39. ) {
  40. continue;
  41. }
  42. if (false === $prevPrice || $this->isFirstPriceBetter($price['website_price'], $prevPrice)) {
  43. $prevPrice = $price['website_price'];
  44. $prevQty = $price['price_qty'];
  45. $priceGroup = $price['cust_group'];
  46. $this->percent = max(0, min(100, 100 - $price['percentage_value']));
  47. }
  48. }
  49. }
  50. return $this->percent;
  51. }
  52. /**
  53. * Returns pricing value
  54. *
  55. * @return bool|float
  56. */
  57. public function getValue()
  58. {
  59. if ($this->value !== null) {
  60. return $this->value;
  61. }
  62. $tierPrice = $this->getDiscountPercent();
  63. if ($tierPrice) {
  64. $regularPrice = $this->getRegularPrice();
  65. $this->value = $regularPrice * ($tierPrice / 100);
  66. } else {
  67. $this->value = false;
  68. }
  69. return $this->value;
  70. }
  71. /**
  72. * Returns regular price
  73. *
  74. * @return bool|float
  75. */
  76. protected function getRegularPrice()
  77. {
  78. return $this->priceInfo->getPrice(RegularPrice::PRICE_CODE)->getValue();
  79. }
  80. /**
  81. * Returns true if first price is better
  82. *
  83. * Method filters tiers price values, higher discount value is better
  84. *
  85. * @param float $firstPrice
  86. * @param float $secondPrice
  87. * @return bool
  88. */
  89. protected function isFirstPriceBetter($firstPrice, $secondPrice)
  90. {
  91. return $firstPrice > $secondPrice;
  92. }
  93. /**
  94. * @return bool
  95. */
  96. public function isPercentageDiscount()
  97. {
  98. return true;
  99. }
  100. }