FinalPriceBox.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\Pricing\Price;
  8. use Magento\Framework\Pricing\Render\PriceBox as BasePriceBox;
  9. use Magento\Msrp\Pricing\Price\MsrpPrice;
  10. use Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface;
  11. use Magento\Framework\View\Element\Template\Context;
  12. use Magento\Framework\Pricing\SaleableInterface;
  13. use Magento\Framework\Pricing\Price\PriceInterface;
  14. use Magento\Framework\Pricing\Render\RendererPool;
  15. use Magento\Framework\App\ObjectManager;
  16. use Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface;
  17. /**
  18. * Class for final_price rendering
  19. *
  20. * @method bool getUseLinkForAsLowAs()
  21. * @method bool getDisplayMinimalPrice()
  22. */
  23. class FinalPriceBox extends BasePriceBox
  24. {
  25. /**
  26. * @var SalableResolverInterface
  27. */
  28. private $salableResolver;
  29. /**
  30. * @var MinimalPriceCalculatorInterface
  31. */
  32. private $minimalPriceCalculator;
  33. /**
  34. * @param Context $context
  35. * @param SaleableInterface $saleableItem
  36. * @param PriceInterface $price
  37. * @param RendererPool $rendererPool
  38. * @param array $data
  39. * @param SalableResolverInterface $salableResolver
  40. * @param MinimalPriceCalculatorInterface $minimalPriceCalculator
  41. */
  42. public function __construct(
  43. Context $context,
  44. SaleableInterface $saleableItem,
  45. PriceInterface $price,
  46. RendererPool $rendererPool,
  47. array $data = [],
  48. SalableResolverInterface $salableResolver = null,
  49. MinimalPriceCalculatorInterface $minimalPriceCalculator = null
  50. ) {
  51. parent::__construct($context, $saleableItem, $price, $rendererPool, $data);
  52. $this->salableResolver = $salableResolver ?: ObjectManager::getInstance()->get(SalableResolverInterface::class);
  53. $this->minimalPriceCalculator = $minimalPriceCalculator
  54. ?: ObjectManager::getInstance()->get(MinimalPriceCalculatorInterface::class);
  55. }
  56. /**
  57. * @return string
  58. */
  59. protected function _toHtml()
  60. {
  61. if (!$this->salableResolver->isSalable($this->getSaleableItem())) {
  62. return '';
  63. }
  64. $result = parent::_toHtml();
  65. //Renders MSRP in case it is enabled
  66. if ($this->isMsrpPriceApplicable()) {
  67. /** @var BasePriceBox $msrpBlock */
  68. $msrpBlock = $this->rendererPool->createPriceRender(
  69. MsrpPrice::PRICE_CODE,
  70. $this->getSaleableItem(),
  71. [
  72. 'real_price_html' => $result,
  73. 'zone' => $this->getZone(),
  74. ]
  75. );
  76. $result = $msrpBlock->toHtml();
  77. }
  78. return $this->wrapResult($result);
  79. }
  80. /**
  81. * Check is MSRP applicable for the current product.
  82. *
  83. * @return bool
  84. */
  85. protected function isMsrpPriceApplicable()
  86. {
  87. try {
  88. /** @var MsrpPrice $msrpPriceType */
  89. $msrpPriceType = $this->getSaleableItem()->getPriceInfo()->getPrice('msrp_price');
  90. } catch (\InvalidArgumentException $e) {
  91. $this->_logger->critical($e);
  92. return false;
  93. }
  94. $product = $this->getSaleableItem();
  95. return $msrpPriceType->canApplyMsrp($product) && $msrpPriceType->isMinimalPriceLessMsrp($product);
  96. }
  97. /**
  98. * Wrap with standard required container
  99. *
  100. * @param string $html
  101. * @return string
  102. */
  103. protected function wrapResult($html)
  104. {
  105. return '<div class="price-box ' . $this->getData('css_classes') . '" ' .
  106. 'data-role="priceBox" ' .
  107. 'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
  108. 'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
  109. '>' . $html . '</div>';
  110. }
  111. /**
  112. * Render minimal amount
  113. *
  114. * @return string
  115. */
  116. public function renderAmountMinimal()
  117. {
  118. $id = $this->getPriceId() ? $this->getPriceId() : 'product-minimal-price-' . $this->getSaleableItem()->getId();
  119. $amount = $this->minimalPriceCalculator->getAmount($this->getSaleableItem());
  120. if ($amount === null) {
  121. return '';
  122. }
  123. return $this->renderAmount(
  124. $amount,
  125. [
  126. 'display_label' => __('As low as'),
  127. 'price_id' => $id,
  128. 'include_container' => false,
  129. 'skip_adjustments' => true
  130. ]
  131. );
  132. }
  133. /**
  134. * Define if the special price should be shown
  135. *
  136. * @return bool
  137. */
  138. public function hasSpecialPrice()
  139. {
  140. $displayRegularPrice = $this->getPriceType(Price\RegularPrice::PRICE_CODE)->getAmount()->getValue();
  141. $displayFinalPrice = $this->getPriceType(Price\FinalPrice::PRICE_CODE)->getAmount()->getValue();
  142. return $displayFinalPrice < $displayRegularPrice;
  143. }
  144. /**
  145. * Define if the minimal price should be shown
  146. *
  147. * @return bool
  148. */
  149. public function showMinimalPrice()
  150. {
  151. $minTierPrice = $this->minimalPriceCalculator->getValue($this->getSaleableItem());
  152. /** @var Price\FinalPrice $finalPrice */
  153. $finalPrice = $this->getPriceType(Price\FinalPrice::PRICE_CODE);
  154. $finalPriceValue = $finalPrice->getAmount()->getValue();
  155. return $this->getDisplayMinimalPrice()
  156. && $minTierPrice !== null
  157. && $minTierPrice < $finalPriceValue;
  158. }
  159. /**
  160. * Get Key for caching block content
  161. *
  162. * @return string
  163. */
  164. public function getCacheKey()
  165. {
  166. return parent::getCacheKey() . ($this->getData('list_category_page') ? '-list-category-page': '');
  167. }
  168. /**
  169. * {@inheritdoc}
  170. *
  171. * @return array
  172. */
  173. public function getCacheKeyInfo()
  174. {
  175. $cacheKeys = parent::getCacheKeyInfo();
  176. $cacheKeys['display_minimal_price'] = $this->getDisplayMinimalPrice();
  177. $cacheKeys['is_product_list'] = $this->isProductList();
  178. return $cacheKeys;
  179. }
  180. /**
  181. * Get flag that price rendering should be done for the list of products
  182. * By default (if flag is not set) is false
  183. *
  184. * @return bool
  185. */
  186. public function isProductList()
  187. {
  188. $isProductList = $this->getData('is_product_list');
  189. return $isProductList === true;
  190. }
  191. }