SpecialPrice.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Adjustment\CalculatorInterface;
  9. use Magento\Framework\Pricing\Price\AbstractPrice;
  10. use Magento\Framework\Pricing\Price\BasePriceProviderInterface;
  11. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  12. use Magento\Store\Api\Data\WebsiteInterface;
  13. /**
  14. * Special price model
  15. */
  16. class SpecialPrice extends AbstractPrice implements SpecialPriceInterface, BasePriceProviderInterface
  17. {
  18. /**
  19. * Price type special
  20. */
  21. const PRICE_CODE = 'special_price';
  22. /**
  23. * @var TimezoneInterface
  24. */
  25. protected $localeDate;
  26. /**
  27. * @param Product $saleableItem
  28. * @param float $quantity
  29. * @param CalculatorInterface $calculator
  30. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  31. * @param TimezoneInterface $localeDate
  32. */
  33. public function __construct(
  34. Product $saleableItem,
  35. $quantity,
  36. CalculatorInterface $calculator,
  37. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
  38. TimezoneInterface $localeDate
  39. ) {
  40. parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
  41. $this->localeDate = $localeDate;
  42. }
  43. /**
  44. * Retrieve special price.
  45. *
  46. * @return bool|float
  47. */
  48. public function getValue()
  49. {
  50. if (null === $this->value) {
  51. $this->value = false;
  52. $specialPrice = $this->getSpecialPrice();
  53. if ($specialPrice !== null && $specialPrice !== false && $this->isScopeDateInInterval()) {
  54. $this->value = (float) $specialPrice;
  55. }
  56. }
  57. return $this->value;
  58. }
  59. /**
  60. * Returns special price
  61. *
  62. * @return float
  63. */
  64. public function getSpecialPrice()
  65. {
  66. $specialPrice = $this->product->getSpecialPrice();
  67. if ($specialPrice !== null && $specialPrice !== false && !$this->isPercentageDiscount()) {
  68. $specialPrice = $this->priceCurrency->convertAndRound($specialPrice);
  69. }
  70. return $specialPrice;
  71. }
  72. /**
  73. * Returns starting date of the special price
  74. *
  75. * @return mixed
  76. */
  77. public function getSpecialFromDate()
  78. {
  79. return $this->product->getSpecialFromDate();
  80. }
  81. /**
  82. * Returns end date of the special price
  83. *
  84. * @return mixed
  85. */
  86. public function getSpecialToDate()
  87. {
  88. return $this->product->getSpecialToDate();
  89. }
  90. /**
  91. * @inheritdoc
  92. */
  93. public function isScopeDateInInterval()
  94. {
  95. return $this->localeDate->isScopeDateInInterval(
  96. WebsiteInterface::ADMIN_CODE,
  97. $this->getSpecialFromDate(),
  98. $this->getSpecialToDate()
  99. );
  100. }
  101. /**
  102. * @inheritdoc
  103. */
  104. public function isPercentageDiscount()
  105. {
  106. return false;
  107. }
  108. }