ConfiguredPrice.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Catalog\Model\Product\Configuration\Item\ItemInterface;
  10. use Magento\Catalog\Pricing\Price as CatalogPrice;
  11. use Magento\Catalog\Pricing\Price\ConfiguredPriceInterface;
  12. use Magento\Catalog\Pricing\Price\ConfiguredPriceSelection;
  13. use Magento\Framework\Pricing\PriceCurrencyInterface;
  14. use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
  15. /**
  16. * Configured price model
  17. * @api
  18. * @since 100.0.2
  19. */
  20. class ConfiguredPrice extends CatalogPrice\FinalPrice implements ConfiguredPriceInterface
  21. {
  22. /**
  23. * Price type configured
  24. */
  25. const PRICE_CODE = self::CONFIGURED_PRICE_CODE;
  26. /**
  27. * @var BundleCalculatorInterface
  28. */
  29. protected $calculator;
  30. /**
  31. * @var null|ItemInterface
  32. */
  33. protected $item;
  34. /**
  35. * Serializer interface instance.
  36. *
  37. * @var JsonSerializer
  38. */
  39. private $serializer;
  40. /**
  41. * @var ConfiguredPriceSelection
  42. */
  43. private $configuredPriceSelection;
  44. /**
  45. * @param Product $saleableItem
  46. * @param float $quantity
  47. * @param BundleCalculatorInterface $calculator
  48. * @param PriceCurrencyInterface $priceCurrency
  49. * @param ItemInterface $item
  50. * @param JsonSerializer|null $serializer
  51. * @param ConfiguredPriceSelection|null $configuredPriceSelection
  52. */
  53. public function __construct(
  54. Product $saleableItem,
  55. $quantity,
  56. BundleCalculatorInterface $calculator,
  57. PriceCurrencyInterface $priceCurrency,
  58. ItemInterface $item = null,
  59. JsonSerializer $serializer = null,
  60. ConfiguredPriceSelection $configuredPriceSelection = null
  61. ) {
  62. $this->item = $item;
  63. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  64. ->get(JsonSerializer::class);
  65. $this->configuredPriceSelection = $configuredPriceSelection
  66. ?: \Magento\Framework\App\ObjectManager::getInstance()
  67. ->get(ConfiguredPriceSelection::class);
  68. parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
  69. }
  70. /**
  71. * @param ItemInterface $item
  72. * @return $this
  73. */
  74. public function setItem(ItemInterface $item)
  75. {
  76. $this->item = $item;
  77. return $this;
  78. }
  79. /**
  80. * Get Options with attached Selections collection.
  81. *
  82. * @return array|\Magento\Bundle\Model\ResourceModel\Option\Collection
  83. */
  84. public function getOptions()
  85. {
  86. $bundleProduct = $this->product;
  87. $bundleOptions = [];
  88. /** @var \Magento\Bundle\Model\Product\Type $typeInstance */
  89. $typeInstance = $bundleProduct->getTypeInstance();
  90. $bundleOptionsIds = [];
  91. if ($this->item !== null) {
  92. // get bundle options
  93. $optionsQuoteItemOption = $this->item->getOptionByCode('bundle_option_ids');
  94. if ($optionsQuoteItemOption && $optionsQuoteItemOption->getValue()) {
  95. $bundleOptionsIds = $this->serializer->unserialize($optionsQuoteItemOption->getValue());
  96. }
  97. }
  98. if ($bundleOptionsIds) {
  99. /** @var \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection */
  100. $optionsCollection = $typeInstance->getOptionsByIds($bundleOptionsIds, $bundleProduct);
  101. // get and add bundle selections collection
  102. $selectionsQuoteItemOption = $this->item->getOptionByCode('bundle_selection_ids');
  103. $bundleSelectionIds = $this->serializer->unserialize($selectionsQuoteItemOption->getValue());
  104. if ($bundleSelectionIds) {
  105. $selectionsCollection = $typeInstance->getSelectionsByIds($bundleSelectionIds, $bundleProduct);
  106. $bundleOptions = $optionsCollection->appendSelections($selectionsCollection, true);
  107. }
  108. }
  109. return $bundleOptions;
  110. }
  111. /**
  112. * Option amount calculation for bundle product.
  113. *
  114. * @param float $baseValue
  115. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  116. */
  117. public function getConfiguredAmount($baseValue = 0.)
  118. {
  119. $selectionPriceList = $this->configuredPriceSelection->getSelectionPriceList($this);
  120. return $this->calculator->calculateBundleAmount(
  121. $baseValue,
  122. $this->product,
  123. $selectionPriceList
  124. );
  125. }
  126. /**
  127. * Get price value
  128. *
  129. * @return float
  130. */
  131. public function getValue()
  132. {
  133. if ($this->item) {
  134. $configuredOptionsAmount = $this->getConfiguredAmount()->getBaseAmount();
  135. return parent::getValue() +
  136. $this->priceInfo
  137. ->getPrice(BundleDiscountPrice::PRICE_CODE)
  138. ->calculateDiscount($configuredOptionsAmount);
  139. }
  140. return parent::getValue();
  141. }
  142. /**
  143. * Get Amount for configured price which is included amount for all selected options
  144. *
  145. * @return \Magento\Framework\Pricing\Amount\AmountInterface
  146. */
  147. public function getAmount()
  148. {
  149. return $this->item ? $this->getConfiguredAmount($this->getBasePrice()->getValue()) : parent::getAmount();
  150. }
  151. }