BundleOptionRegularPrice.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Bundle\Pricing\Price;
  8. use Magento\Bundle\Pricing\Adjustment\BundleCalculatorInterface;
  9. use Magento\Catalog\Model\Product;
  10. use Magento\Framework\Pricing\Price\AbstractPrice;
  11. use Magento\Framework\Pricing\PriceCurrencyInterface;
  12. /**
  13. * Bundle option price model with final price.
  14. */
  15. class BundleOptionRegularPrice extends AbstractPrice implements BundleOptionPriceInterface
  16. {
  17. /**
  18. * Price model code.
  19. */
  20. const PRICE_CODE = 'bundle_option_regular_price';
  21. /**
  22. * @var BundleCalculatorInterface
  23. */
  24. protected $calculator;
  25. /**
  26. * @var BundleOptions
  27. */
  28. private $bundleOptions;
  29. /**
  30. * @param Product $saleableItem
  31. * @param float $quantity
  32. * @param BundleCalculatorInterface $calculator
  33. * @param PriceCurrencyInterface $priceCurrency
  34. * @param BundleOptions $bundleOptions
  35. */
  36. public function __construct(
  37. Product $saleableItem,
  38. $quantity,
  39. BundleCalculatorInterface $calculator,
  40. PriceCurrencyInterface $priceCurrency,
  41. BundleOptions $bundleOptions
  42. ) {
  43. parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
  44. $this->product->setQty($this->quantity);
  45. $this->bundleOptions = $bundleOptions;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getValue()
  51. {
  52. if (null === $this->value) {
  53. $this->value = $this->bundleOptions->calculateOptions($this->product);
  54. }
  55. return $this->value;
  56. }
  57. /**
  58. * Get Options with attached Selections collection.
  59. *
  60. * @return \Magento\Bundle\Model\ResourceModel\Option\Collection
  61. */
  62. public function getOptions() : \Magento\Bundle\Model\ResourceModel\Option\Collection
  63. {
  64. return $this->bundleOptions->getOptions($this->product);
  65. }
  66. /**
  67. * Get selection amount.
  68. *
  69. * @param \Magento\Bundle\Model\Selection $selection
  70. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  71. */
  72. public function getOptionSelectionAmount($selection) : \Magento\Framework\Pricing\Amount\AmountInterface
  73. {
  74. return $this->bundleOptions->getOptionSelectionAmount(
  75. $this->product,
  76. $selection,
  77. true
  78. );
  79. }
  80. /**
  81. * Get minimal amount of bundle price with options.
  82. *
  83. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  84. */
  85. public function getAmount() : \Magento\Framework\Pricing\Amount\AmountInterface
  86. {
  87. return $this->calculator->getOptionsAmount($this->product);
  88. }
  89. }