BundleOptionPrice.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\Bundle\Pricing\Adjustment\BundleCalculatorInterface;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Framework\Pricing\Price\AbstractPrice;
  10. use Magento\Framework\App\ObjectManager;
  11. /**
  12. * Bundle option price model with final price.
  13. */
  14. class BundleOptionPrice extends AbstractPrice implements BundleOptionPriceInterface
  15. {
  16. /**
  17. * Price model code
  18. */
  19. const PRICE_CODE = 'bundle_option';
  20. /**
  21. * @var BundleCalculatorInterface
  22. */
  23. protected $calculator;
  24. /**
  25. * @var BundleSelectionFactory
  26. * @deprecated 100.2.3
  27. */
  28. protected $selectionFactory;
  29. /**
  30. * @var float|bool|null
  31. */
  32. protected $maximalPrice;
  33. /**
  34. * @var BundleOptions
  35. */
  36. private $bundleOptions;
  37. /**
  38. * @param Product $saleableItem
  39. * @param float $quantity
  40. * @param BundleCalculatorInterface $calculator
  41. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  42. * @param BundleSelectionFactory $bundleSelectionFactory
  43. * @param BundleOptions|null $bundleOptions
  44. */
  45. public function __construct(
  46. Product $saleableItem,
  47. $quantity,
  48. BundleCalculatorInterface $calculator,
  49. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
  50. BundleSelectionFactory $bundleSelectionFactory,
  51. BundleOptions $bundleOptions = null
  52. ) {
  53. $this->selectionFactory = $bundleSelectionFactory;
  54. parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
  55. $this->product->setQty($this->quantity);
  56. $this->bundleOptions = $bundleOptions ?: ObjectManager::getInstance()->get(BundleOptions::class);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getValue()
  62. {
  63. if (null === $this->value) {
  64. $this->value = $this->bundleOptions->calculateOptions($this->product);
  65. }
  66. return $this->value;
  67. }
  68. /**
  69. * Getter for maximal price of options.
  70. *
  71. * @return bool|float
  72. * @deprecated 100.2.3
  73. */
  74. public function getMaxValue()
  75. {
  76. if (null === $this->maximalPrice) {
  77. $this->maximalPrice = $this->bundleOptions->calculateOptions($this->product, false);
  78. }
  79. return $this->maximalPrice;
  80. }
  81. /**
  82. * Get Options with attached Selections collection.
  83. *
  84. * @return \Magento\Bundle\Model\ResourceModel\Option\Collection
  85. */
  86. public function getOptions()
  87. {
  88. return $this->bundleOptions->getOptions($this->product);
  89. }
  90. /**
  91. * Get selection amount.
  92. *
  93. * @param \Magento\Bundle\Model\Selection $selection
  94. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  95. */
  96. public function getOptionSelectionAmount($selection)
  97. {
  98. return $this->bundleOptions->getOptionSelectionAmount(
  99. $this->product,
  100. $selection,
  101. false
  102. );
  103. }
  104. /**
  105. * Calculate maximal or minimal options value.
  106. *
  107. * @param bool $searchMin
  108. * @return bool|float
  109. */
  110. protected function calculateOptions($searchMin = true)
  111. {
  112. return $this->bundleOptions->calculateOptions($this->product, $searchMin);
  113. }
  114. /**
  115. * Get minimal amount of bundle price with options
  116. *
  117. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  118. */
  119. public function getAmount()
  120. {
  121. return $this->calculator->getOptionsAmount($this->product);
  122. }
  123. }