Render.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing;
  7. use Magento\Framework\Pricing\Amount\AmountInterface;
  8. use Magento\Framework\Pricing\Price\PriceInterface;
  9. use Magento\Framework\Pricing\Render\Layout;
  10. use Magento\Framework\View\Element\AbstractBlock;
  11. use Magento\Framework\View\Element\Template;
  12. /**
  13. * Base price render
  14. *
  15. * @api
  16. * @method string getPriceRenderHandle()
  17. *
  18. * @api
  19. * @since 100.0.2
  20. */
  21. class Render extends AbstractBlock
  22. {
  23. /**#@+
  24. * Zones where prices displaying can be configured
  25. */
  26. const ZONE_ITEM_VIEW = 'item_view';
  27. const ZONE_ITEM_LIST = 'item_list';
  28. const ZONE_ITEM_OPTION = 'item_option';
  29. const ZONE_SALES = 'sales';
  30. const ZONE_EMAIL = 'email';
  31. const ZONE_CART = 'cart';
  32. const ZONE_DEFAULT = null;
  33. /**#@-*/
  34. /**#@-*/
  35. protected $defaultTypeRender = 'default';
  36. /**
  37. * Price layout
  38. *
  39. * @var Layout
  40. */
  41. protected $priceLayout;
  42. /**
  43. * Constructor
  44. *
  45. * @param Template\Context $context
  46. * @param Layout $priceLayout
  47. * @param array $data
  48. */
  49. public function __construct(
  50. Template\Context $context,
  51. Layout $priceLayout,
  52. array $data = []
  53. ) {
  54. $this->priceLayout = $priceLayout;
  55. parent::__construct($context, $data);
  56. }
  57. /**
  58. * Prepare layout
  59. *
  60. * @return $this
  61. */
  62. protected function _prepareLayout()
  63. {
  64. $this->priceLayout->addHandle($this->getPriceRenderHandle());
  65. $this->priceLayout->loadLayout();
  66. return parent::_prepareLayout();
  67. }
  68. /**
  69. * Render price
  70. *
  71. * @param string $priceCode
  72. * @param SaleableInterface $saleableItem
  73. * @param array $arguments
  74. * @return string
  75. * @throws \InvalidArgumentException
  76. * @throws \RuntimeException
  77. */
  78. public function render($priceCode, SaleableInterface $saleableItem, array $arguments = [])
  79. {
  80. $useArguments = array_replace($this->_data, $arguments);
  81. /** @var \Magento\Framework\Pricing\Render\RendererPool $rendererPool */
  82. $rendererPool = $this->priceLayout->getBlock('render.product.prices');
  83. if (!$rendererPool) {
  84. throw new \RuntimeException('Wrong Price Rendering layout configuration. Factory block is missed');
  85. }
  86. // obtain concrete Price Render
  87. $priceRender = $rendererPool->createPriceRender($priceCode, $saleableItem, $useArguments);
  88. return $priceRender->toHtml();
  89. }
  90. /**
  91. * Render price amount
  92. *
  93. * @param AmountInterface $amount
  94. * @param PriceInterface $price
  95. * @param SaleableInterface $saleableItem
  96. * @param array $arguments
  97. * @return string
  98. * @throws \RuntimeException
  99. */
  100. public function renderAmount(
  101. AmountInterface $amount,
  102. PriceInterface $price,
  103. SaleableInterface $saleableItem = null,
  104. array $arguments = []
  105. ) {
  106. $useArguments = array_replace($this->_data, $arguments);
  107. /** @var \Magento\Framework\Pricing\Render\RendererPool $rendererPool */
  108. $rendererPool = $this->priceLayout->getBlock('render.product.prices');
  109. if (!$rendererPool) {
  110. throw new \RuntimeException('Wrong Price Rendering layout configuration. Factory block is missed');
  111. }
  112. // obtain concrete Amount Render
  113. $amountRender = $rendererPool->createAmountRender($amount, $saleableItem, $price, $useArguments);
  114. return $amountRender->toHtml();
  115. }
  116. }