PriceBox.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Render;
  7. use Magento\Framework\DataObject\IdentityInterface;
  8. use Magento\Framework\Pricing\Amount\AmountInterface;
  9. use Magento\Framework\Pricing\SaleableInterface;
  10. use Magento\Framework\Pricing\Price\PriceInterface;
  11. use Magento\Framework\View\Element\Template;
  12. /**
  13. * Default price box renderer
  14. *
  15. * @method bool hasListClass()
  16. * @method string getListClass()
  17. */
  18. class PriceBox extends Template implements PriceBoxRenderInterface, IdentityInterface
  19. {
  20. /** Default block lifetime */
  21. const DEFAULT_LIFETIME = 3600;
  22. /**
  23. * @var SaleableInterface
  24. */
  25. protected $saleableItem;
  26. /**
  27. * @var PriceInterface
  28. */
  29. protected $price;
  30. /**
  31. * @var RendererPool
  32. */
  33. protected $rendererPool;
  34. /**
  35. * @param Template\Context $context
  36. * @param SaleableInterface $saleableItem
  37. * @param PriceInterface $price
  38. * @param RendererPool $rendererPool
  39. * @param array $data
  40. */
  41. public function __construct(
  42. Template\Context $context,
  43. SaleableInterface $saleableItem,
  44. PriceInterface $price,
  45. RendererPool $rendererPool,
  46. array $data = []
  47. ) {
  48. $this->saleableItem = $saleableItem;
  49. $this->price = $price;
  50. $this->rendererPool = $rendererPool;
  51. parent::__construct($context, $data);
  52. }
  53. /**
  54. * @return string
  55. */
  56. protected function _toHtml()
  57. {
  58. $cssClasses = $this->hasData('css_classes') ? explode(' ', $this->getData('css_classes')) : [];
  59. $cssClasses[] = 'price-' . $this->getPrice()->getPriceCode();
  60. $this->setData('css_classes', implode(' ', $cssClasses));
  61. return parent::_toHtml();
  62. }
  63. /**
  64. * Get Key for caching block content
  65. *
  66. * @return string
  67. */
  68. public function getCacheKey()
  69. {
  70. return parent::getCacheKey() . '-' . $this->getPriceId() . '-' . $this->getPrice()->getPriceCode();
  71. }
  72. /**
  73. * Get block cache life time
  74. *
  75. * @return int
  76. */
  77. protected function getCacheLifetime()
  78. {
  79. return parent::hasCacheLifetime() ? parent::getCacheLifetime() : null;
  80. }
  81. /**
  82. * @return SaleableInterface
  83. */
  84. public function getSaleableItem()
  85. {
  86. return $this->saleableItem;
  87. }
  88. /**
  89. * @return PriceInterface
  90. */
  91. public function getPrice()
  92. {
  93. return $this->price;
  94. }
  95. /**
  96. * Get price id
  97. *
  98. * @param null|string $defaultPrefix
  99. * @param null|string $defaultSuffix
  100. * @return string
  101. */
  102. public function getPriceId($defaultPrefix = null, $defaultSuffix = null)
  103. {
  104. if ($this->hasData('price_id')) {
  105. return $this->getData('price_id');
  106. }
  107. $priceId = $this->saleableItem->getId();
  108. $prefix = $this->hasData('price_id_prefix') ? $this->getData('price_id_prefix') : $defaultPrefix;
  109. $suffix = $this->hasData('price_id_suffix') ? $this->getData('price_id_suffix') : $defaultSuffix;
  110. $priceId = $prefix . $priceId . $suffix;
  111. return $priceId;
  112. }
  113. /**
  114. * Retrieve price object of given type and quantity
  115. *
  116. * @param string $priceCode
  117. * @return PriceInterface
  118. */
  119. public function getPriceType($priceCode)
  120. {
  121. return $this->saleableItem->getPriceInfo()->getPrice($priceCode);
  122. }
  123. /**
  124. * @param AmountInterface $amount
  125. * @param array $arguments
  126. * @return string
  127. */
  128. public function renderAmount(AmountInterface $amount, array $arguments = [])
  129. {
  130. $arguments = array_replace($this->getData(), $arguments);
  131. //@TODO AmountInterface does not contain toHtml() method
  132. return $this->getAmountRender($amount, $arguments)->toHtml();
  133. }
  134. /**
  135. * @param AmountInterface $amount
  136. * @param array $arguments
  137. * @return AmountRenderInterface
  138. */
  139. protected function getAmountRender(AmountInterface $amount, array $arguments = [])
  140. {
  141. return $this->rendererPool->createAmountRender(
  142. $amount,
  143. $this->getSaleableItem(),
  144. $this->getPrice(),
  145. $arguments
  146. );
  147. }
  148. /**
  149. * @return RendererPool
  150. */
  151. public function getRendererPool()
  152. {
  153. return $this->rendererPool;
  154. }
  155. /**
  156. * Return unique ID(s) for each object in system
  157. *
  158. * @return array
  159. */
  160. public function getIdentities()
  161. {
  162. $item = $this->getSaleableItem();
  163. if ($item instanceof IdentityInterface) {
  164. return $item->getIdentities();
  165. } else {
  166. return [];
  167. }
  168. }
  169. }