Render.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Pricing;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Framework\Pricing\SaleableInterface;
  9. use Magento\Framework\Pricing\Render as PricingRender;
  10. use Magento\Framework\Registry;
  11. use Magento\Framework\View\Element\Template;
  12. /**
  13. * Catalog Price Render
  14. *
  15. * @api
  16. * @method string getPriceRender()
  17. * @method string getPriceTypeCode()
  18. * @since 100.0.2
  19. */
  20. class Render extends Template
  21. {
  22. /**
  23. * @var \Magento\Framework\Registry
  24. */
  25. protected $registry;
  26. /**
  27. * Construct
  28. *
  29. * @param Template\Context $context
  30. * @param Registry $registry
  31. * @param array $data
  32. */
  33. public function __construct(
  34. Template\Context $context,
  35. Registry $registry,
  36. array $data = []
  37. ) {
  38. $this->registry = $registry;
  39. parent::__construct($context, $data);
  40. }
  41. /**
  42. * Produce and return block's html output
  43. *
  44. * @return string
  45. */
  46. protected function _toHtml()
  47. {
  48. /** @var PricingRender $priceRender */
  49. $priceRender = $this->getLayout()->getBlock($this->getPriceRender());
  50. if ($priceRender instanceof PricingRender) {
  51. $product = $this->getProduct();
  52. if ($product instanceof SaleableInterface) {
  53. $arguments = $this->getData();
  54. $arguments['render_block'] = $this;
  55. return $priceRender->render($this->getPriceTypeCode(), $product, $arguments);
  56. }
  57. }
  58. return parent::_toHtml();
  59. }
  60. /**
  61. * Returns saleable item instance
  62. *
  63. * @return Product
  64. */
  65. protected function getProduct()
  66. {
  67. $parentBlock = $this->getParentBlock();
  68. $product = $parentBlock && $parentBlock->getProductItem()
  69. ? $parentBlock->getProductItem()
  70. : $this->registry->registry('product');
  71. return $product;
  72. }
  73. }