FinalPrice.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Pricing\Price;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Framework\Pricing\Price\AbstractPrice;
  9. /**
  10. * Final price model
  11. */
  12. class FinalPrice extends AbstractPrice implements FinalPriceInterface
  13. {
  14. /**
  15. * Price type final
  16. */
  17. const PRICE_CODE = 'final_price';
  18. /**
  19. * @var BasePrice
  20. */
  21. private $basePrice;
  22. /**
  23. * @var \Magento\Framework\Pricing\Amount\AmountInterface
  24. */
  25. protected $minimalPrice;
  26. /**
  27. * @var \Magento\Framework\Pricing\Amount\AmountInterface
  28. */
  29. protected $maximalPrice;
  30. /**
  31. * Get Value
  32. *
  33. * @return float|bool
  34. */
  35. public function getValue()
  36. {
  37. return max(0, $this->getBasePrice()->getValue());
  38. }
  39. /**
  40. * Get Minimal Price Amount
  41. *
  42. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  43. */
  44. public function getMinimalPrice()
  45. {
  46. if (!$this->minimalPrice) {
  47. $minimalPrice = $this->product->getMinimalPrice();
  48. if ($minimalPrice === null) {
  49. $minimalPrice = $this->getValue();
  50. } else {
  51. $minimalPrice = $this->priceCurrency->convertAndRound($minimalPrice);
  52. }
  53. $this->minimalPrice = $this->calculator->getAmount($minimalPrice, $this->product);
  54. }
  55. return $this->minimalPrice;
  56. }
  57. /**
  58. * Get Maximal Price Amount
  59. *
  60. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  61. */
  62. public function getMaximalPrice()
  63. {
  64. if (!$this->maximalPrice) {
  65. $maximalPrice = $this->product->getMaximalPrice();
  66. if ($maximalPrice === null) {
  67. $maximalPrice = $this->getValue();
  68. } else {
  69. $maximalPrice = $this->priceCurrency->convertAndRound($maximalPrice);
  70. }
  71. $this->maximalPrice = $this->calculator->getAmount($maximalPrice, $this->product);
  72. }
  73. return $this->maximalPrice;
  74. }
  75. /**
  76. * Retrieve base price instance lazily
  77. *
  78. * @return BasePrice|\Magento\Framework\Pricing\Price\PriceInterface
  79. */
  80. protected function getBasePrice()
  81. {
  82. if (!$this->basePrice) {
  83. $this->basePrice = $this->priceInfo->getPrice(BasePrice::PRICE_CODE);
  84. }
  85. return $this->basePrice;
  86. }
  87. }