ConfigurableRegularPrice.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Pricing\Price;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Pricing\Price\AbstractPrice;
  10. /**
  11. * Class RegularPrice
  12. */
  13. class ConfigurableRegularPrice extends AbstractPrice implements ConfigurableRegularPriceInterface
  14. {
  15. /**
  16. * Price type
  17. */
  18. const PRICE_CODE = 'regular_price';
  19. /**
  20. * @var \Magento\Framework\Pricing\Amount\AmountInterface
  21. */
  22. protected $maxRegularAmount;
  23. /**
  24. * @var \Magento\Framework\Pricing\Amount\AmountInterface
  25. */
  26. protected $minRegularAmount;
  27. /**
  28. * @var array
  29. */
  30. protected $values = [];
  31. /**
  32. * @var \Magento\ConfigurableProduct\Pricing\Price\PriceResolverInterface
  33. */
  34. protected $priceResolver;
  35. /**
  36. * @var ConfigurableOptionsProviderInterface
  37. */
  38. private $configurableOptionsProvider;
  39. /**
  40. * @var LowestPriceOptionsProviderInterface
  41. */
  42. private $lowestPriceOptionsProvider;
  43. /**
  44. * @param \Magento\Framework\Pricing\SaleableInterface $saleableItem
  45. * @param float $quantity
  46. * @param \Magento\Framework\Pricing\Adjustment\CalculatorInterface $calculator
  47. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  48. * @param PriceResolverInterface $priceResolver
  49. * @param LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider
  50. */
  51. public function __construct(
  52. \Magento\Framework\Pricing\SaleableInterface $saleableItem,
  53. $quantity,
  54. \Magento\Framework\Pricing\Adjustment\CalculatorInterface $calculator,
  55. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
  56. PriceResolverInterface $priceResolver,
  57. LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider = null
  58. ) {
  59. parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
  60. $this->priceResolver = $priceResolver;
  61. $this->lowestPriceOptionsProvider = $lowestPriceOptionsProvider ?:
  62. ObjectManager::getInstance()->get(LowestPriceOptionsProviderInterface::class);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getValue()
  68. {
  69. if (!isset($this->values[$this->product->getId()])) {
  70. $this->values[$this->product->getId()] = $this->priceResolver->resolvePrice($this->product);
  71. }
  72. return $this->values[$this->product->getId()];
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getAmount()
  78. {
  79. return $this->getMinRegularAmount();
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getMaxRegularAmount()
  85. {
  86. if (null === $this->maxRegularAmount) {
  87. $this->maxRegularAmount = $this->doGetMaxRegularAmount() ?: false;
  88. }
  89. return $this->maxRegularAmount;
  90. }
  91. /**
  92. * Get max regular amount
  93. *
  94. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  95. */
  96. protected function doGetMaxRegularAmount()
  97. {
  98. $maxAmount = null;
  99. foreach ($this->getUsedProducts() as $product) {
  100. $childPriceAmount = $product->getPriceInfo()->getPrice(self::PRICE_CODE)->getAmount();
  101. if (!$maxAmount || ($childPriceAmount->getValue() > $maxAmount->getValue())) {
  102. $maxAmount = $childPriceAmount;
  103. }
  104. }
  105. return $maxAmount;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getMinRegularAmount()
  111. {
  112. if (null === $this->minRegularAmount) {
  113. $this->minRegularAmount = $this->doGetMinRegularAmount() ?: parent::getAmount();
  114. }
  115. return $this->minRegularAmount;
  116. }
  117. /**
  118. * Get min regular amount
  119. *
  120. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  121. */
  122. protected function doGetMinRegularAmount()
  123. {
  124. $minAmount = null;
  125. foreach ($this->lowestPriceOptionsProvider->getProducts($this->product) as $product) {
  126. $childPriceAmount = $product->getPriceInfo()->getPrice(self::PRICE_CODE)->getAmount();
  127. if (!$minAmount || ($childPriceAmount->getValue() < $minAmount->getValue())) {
  128. $minAmount = $childPriceAmount;
  129. }
  130. }
  131. return $minAmount;
  132. }
  133. /**
  134. * Get children simple products
  135. *
  136. * @return Product[]
  137. */
  138. protected function getUsedProducts()
  139. {
  140. return $this->getConfigurableOptionsProvider()->getProducts($this->product);
  141. }
  142. /**
  143. * @return \Magento\ConfigurableProduct\Pricing\Price\ConfigurableOptionsProviderInterface
  144. * @deprecated 100.1.1
  145. */
  146. private function getConfigurableOptionsProvider()
  147. {
  148. if (null === $this->configurableOptionsProvider) {
  149. $this->configurableOptionsProvider = ObjectManager::getInstance()
  150. ->get(ConfigurableOptionsProviderInterface::class);
  151. }
  152. return $this->configurableOptionsProvider;
  153. }
  154. }