FinalPrice.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\Model\Product;
  8. use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
  9. use Magento\Catalog\Pricing\Price\CustomOptionPrice;
  10. use Magento\Bundle\Model\Product\Price;
  11. use Magento\Framework\App\ObjectManager;
  12. use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
  13. /**
  14. * Final price model
  15. */
  16. class FinalPrice extends \Magento\Catalog\Pricing\Price\FinalPrice implements FinalPriceInterface
  17. {
  18. /**
  19. * @var \Magento\Framework\Pricing\Amount\AmountInterface
  20. */
  21. protected $maximalPrice;
  22. /**
  23. * @var \Magento\Framework\Pricing\Amount\AmountInterface
  24. */
  25. protected $minimalPrice;
  26. /**
  27. * @var \Magento\Framework\Pricing\Amount\AmountInterface
  28. */
  29. protected $priceWithoutOption;
  30. /**
  31. * @var \Magento\Bundle\Pricing\Price\BundleOptionPrice
  32. */
  33. protected $bundleOptionPrice;
  34. /**
  35. * @var \Magento\Catalog\Api\ProductCustomOptionRepositoryInterface
  36. */
  37. private $productOptionRepository;
  38. /**
  39. * @param Product $saleableItem
  40. * @param float $quantity
  41. * @param CalculatorInterface $calculator
  42. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  43. */
  44. public function __construct(
  45. Product $saleableItem,
  46. $quantity,
  47. CalculatorInterface $calculator,
  48. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  49. ) {
  50. parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
  51. }
  52. /**
  53. * Returns price value
  54. *
  55. * @return float
  56. */
  57. public function getValue()
  58. {
  59. return parent::getValue() +
  60. $this->getBundleOptionPrice()->getValue();
  61. }
  62. /**
  63. * Returns max price
  64. *
  65. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  66. */
  67. public function getMaximalPrice()
  68. {
  69. if (!$this->maximalPrice) {
  70. $price = $this->getBasePrice()->getValue();
  71. if ($this->product->getPriceType() == Price::PRICE_TYPE_FIXED) {
  72. /** @var \Magento\Catalog\Pricing\Price\CustomOptionPrice $customOptionPrice */
  73. $customOptionPrice = $this->priceInfo->getPrice(CustomOptionPrice::PRICE_CODE);
  74. $price += $customOptionPrice->getCustomOptionRange(false);
  75. }
  76. $this->maximalPrice = $this->calculator->getMaxAmount($price, $this->product);
  77. }
  78. return $this->maximalPrice;
  79. }
  80. /**
  81. * Return ProductCustomOptionRepository
  82. *
  83. * @return ProductCustomOptionRepositoryInterface
  84. * @deprecated 100.1.0
  85. */
  86. private function getProductOptionRepository()
  87. {
  88. if (!$this->productOptionRepository) {
  89. $this->productOptionRepository = ObjectManager::getInstance()->get(
  90. ProductCustomOptionRepositoryInterface::class
  91. );
  92. }
  93. return $this->productOptionRepository;
  94. }
  95. /**
  96. * Returns min price
  97. *
  98. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  99. */
  100. public function getMinimalPrice()
  101. {
  102. return $this->getAmount();
  103. }
  104. /**
  105. * Returns price amount
  106. *
  107. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  108. */
  109. public function getAmount()
  110. {
  111. if (!$this->minimalPrice) {
  112. $price = parent::getValue();
  113. if ($this->product->getPriceType() == Price::PRICE_TYPE_FIXED) {
  114. $this->loadProductCustomOptions();
  115. /** @var \Magento\Catalog\Pricing\Price\CustomOptionPrice $customOptionPrice */
  116. $customOptionPrice = $this->priceInfo->getPrice(CustomOptionPrice::PRICE_CODE);
  117. $price += $customOptionPrice->getCustomOptionRange(true);
  118. }
  119. $this->minimalPrice = $this->calculator->getAmount($price, $this->product);
  120. }
  121. return $this->minimalPrice;
  122. }
  123. /**
  124. * Load product custom options
  125. *
  126. * @return void
  127. */
  128. private function loadProductCustomOptions()
  129. {
  130. if (!$this->product->getOptions()) {
  131. $options = [];
  132. foreach ($this->getProductOptionRepository()->getProductOptions($this->product) as $option) {
  133. $option->setProduct($this->product);
  134. $options[] = $option;
  135. }
  136. $this->product->setOptions($options);
  137. }
  138. }
  139. /**
  140. * get bundle product price without any option
  141. *
  142. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  143. */
  144. public function getPriceWithoutOption()
  145. {
  146. if (!$this->priceWithoutOption) {
  147. $this->priceWithoutOption = $this->calculator->getAmountWithoutOption(parent::getValue(), $this->product);
  148. }
  149. return $this->priceWithoutOption;
  150. }
  151. /**
  152. * Returns option price
  153. *
  154. * @return \Magento\Bundle\Pricing\Price\BundleOptionPrice
  155. */
  156. protected function getBundleOptionPrice()
  157. {
  158. if (!$this->bundleOptionPrice) {
  159. $this->bundleOptionPrice = $this->priceInfo->getPrice(BundleOptionPrice::PRICE_CODE);
  160. }
  161. return $this->bundleOptionPrice;
  162. }
  163. }