ConfiguredPriceBox.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Pricing\Render;
  7. use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
  8. use Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface;
  9. use Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface;
  10. use Magento\Framework\Pricing\Price\PriceInterface;
  11. use Magento\Catalog\Pricing\Price\ConfiguredPriceInterface;
  12. use Magento\Catalog\Pricing\Price\FinalPrice;
  13. use Magento\Catalog\Pricing\Price\RegularPrice;
  14. use Magento\Framework\Pricing\Render\RendererPool;
  15. use Magento\Framework\Pricing\SaleableInterface;
  16. use Magento\Framework\View\Element\Template\Context;
  17. use Magento\Catalog\Pricing\Price\ConfiguredPriceSelection;
  18. use Magento\Framework\App\ObjectManager;
  19. /**
  20. * Class for configured_price rendering.
  21. */
  22. class ConfiguredPriceBox extends FinalPriceBox
  23. {
  24. /**
  25. * @var ConfiguredPriceSelection
  26. */
  27. private $configuredPriceSelection;
  28. /**
  29. * @param Context $context
  30. * @param SaleableInterface $saleableItem
  31. * @param PriceInterface $price
  32. * @param RendererPool $rendererPool
  33. * @param array $data
  34. * @param SalableResolverInterface|null $salableResolver
  35. * @param MinimalPriceCalculatorInterface|null $minimalPriceCalculator
  36. * @param ConfiguredPriceSelection|null $configuredPriceSelection
  37. */
  38. public function __construct(
  39. Context $context,
  40. SaleableInterface $saleableItem,
  41. PriceInterface $price,
  42. RendererPool $rendererPool,
  43. array $data = [],
  44. SalableResolverInterface $salableResolver = null,
  45. MinimalPriceCalculatorInterface $minimalPriceCalculator = null,
  46. ConfiguredPriceSelection $configuredPriceSelection = null
  47. ) {
  48. $this->configuredPriceSelection = $configuredPriceSelection
  49. ?: ObjectManager::getInstance()
  50. ->get(ConfiguredPriceSelection::class);
  51. parent::__construct(
  52. $context,
  53. $saleableItem,
  54. $price,
  55. $rendererPool,
  56. $data,
  57. $salableResolver,
  58. $minimalPriceCalculator
  59. );
  60. }
  61. /**
  62. * Retrieve an item instance to the configured price model
  63. *
  64. * @return $this
  65. */
  66. protected function _prepareLayout()
  67. {
  68. /** @var $price \Magento\Catalog\Pricing\Price\ConfiguredPrice */
  69. $price = $this->getPrice();
  70. /** @var $renderBlock \Magento\Catalog\Pricing\Render */
  71. $renderBlock = $this->getRenderBlock();
  72. if ($renderBlock && $renderBlock->getItem() instanceof ItemInterface) {
  73. $price->setItem($renderBlock->getItem());
  74. } elseif ($renderBlock
  75. && $renderBlock->getParentBlock()
  76. && $renderBlock->getParentBlock()->getItem() instanceof ItemInterface
  77. ) {
  78. $price->setItem($renderBlock->getParentBlock()->getItem());
  79. }
  80. return parent::_prepareLayout();
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getPriceType($priceCode)
  86. {
  87. $price = $this->saleableItem->getPriceInfo()->getPrice($priceCode);
  88. $item = $this->getData('item');
  89. if ($price instanceof ConfiguredPriceInterface
  90. && $item instanceof ItemInterface
  91. ) {
  92. $price->setItem($item);
  93. }
  94. return $price;
  95. }
  96. /**
  97. * @return PriceInterface
  98. */
  99. public function getConfiguredPrice(): PriceInterface
  100. {
  101. /** @var \Magento\Bundle\Pricing\Price\ConfiguredPrice $configuredPrice */
  102. $configuredPrice = $this->getPrice();
  103. if (empty($this->configuredPriceSelection->getSelectionPriceList($configuredPrice))) {
  104. // If there was no selection we must show minimal regular price
  105. return $this->getSaleableItem()->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE);
  106. }
  107. return $configuredPrice;
  108. }
  109. /**
  110. * @return PriceInterface
  111. */
  112. public function getConfiguredRegularPrice(): PriceInterface
  113. {
  114. /** @var \Magento\Bundle\Pricing\Price\ConfiguredPrice $configuredPrice */
  115. $configuredPrice = $this->getPriceType(ConfiguredPriceInterface::CONFIGURED_REGULAR_PRICE_CODE);
  116. if (empty($this->configuredPriceSelection->getSelectionPriceList($configuredPrice))) {
  117. // If there was no selection we must show minimal regular price
  118. return $this->getSaleableItem()->getPriceInfo()->getPrice(RegularPrice::PRICE_CODE);
  119. }
  120. return $configuredPrice;
  121. }
  122. /**
  123. * Define if the special price should be shown.
  124. *
  125. * @return bool
  126. */
  127. public function hasSpecialPrice(): bool
  128. {
  129. if ($this->price->getPriceCode() == ConfiguredPriceInterface::CONFIGURED_PRICE_CODE) {
  130. $displayRegularPrice = $this->getConfiguredRegularPrice()->getAmount()->getValue();
  131. $displayFinalPrice = $this->getConfiguredPrice()->getAmount()->getValue();
  132. return $displayFinalPrice < $displayRegularPrice;
  133. }
  134. return parent::hasSpecialPrice();
  135. }
  136. }