Price.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget\Grid\Column\Renderer;
  7. /**
  8. * Backend grid item renderer currency
  9. *
  10. * @api
  11. * @deprecated 100.2.0 in favour of UI component implementation
  12. * @since 100.0.2
  13. */
  14. class Price extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
  15. {
  16. /**
  17. * @var int
  18. */
  19. protected $_defaultWidth = 100;
  20. /**
  21. * Currency objects cache
  22. *
  23. * @var \Magento\Framework\DataObject[]
  24. */
  25. protected static $_currencies = [];
  26. /**
  27. * @var \Magento\Framework\Locale\CurrencyInterface
  28. */
  29. protected $_localeCurrency;
  30. /**
  31. * @param \Magento\Backend\Block\Context $context
  32. * @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
  33. * @param array $data
  34. */
  35. public function __construct(
  36. \Magento\Backend\Block\Context $context,
  37. \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
  38. array $data = []
  39. ) {
  40. parent::__construct($context, $data);
  41. $this->_localeCurrency = $localeCurrency;
  42. }
  43. /**
  44. * Renders grid column
  45. *
  46. * @param \Magento\Framework\DataObject $row
  47. * @return string
  48. */
  49. public function render(\Magento\Framework\DataObject $row)
  50. {
  51. if ($data = $this->_getValue($row)) {
  52. $currencyCode = $this->_getCurrencyCode($row);
  53. if (!$currencyCode) {
  54. return $data;
  55. }
  56. $data = (float)$data * $this->_getRate($row);
  57. $data = sprintf("%f", $data);
  58. $data = $this->_localeCurrency->getCurrency($currencyCode)->toCurrency($data);
  59. return $data;
  60. }
  61. return $this->getColumn()->getDefault();
  62. }
  63. /**
  64. * Returns currency code for the row, false on error
  65. *
  66. * @param \Magento\Framework\DataObject $row
  67. * @return string|false
  68. */
  69. protected function _getCurrencyCode($row)
  70. {
  71. if ($code = $this->getColumn()->getCurrencyCode()) {
  72. return $code;
  73. }
  74. if ($code = $row->getData($this->getColumn()->getCurrency())) {
  75. return $code;
  76. }
  77. return false;
  78. }
  79. /**
  80. * Returns rate for the row, 1 by default
  81. *
  82. * @param \Magento\Framework\DataObject $row
  83. * @return float|int
  84. */
  85. protected function _getRate($row)
  86. {
  87. if ($rate = $this->getColumn()->getRate()) {
  88. return (float)$rate;
  89. }
  90. if ($rate = $row->getData($this->getColumn()->getRateField())) {
  91. return (float)$rate;
  92. }
  93. return 1;
  94. }
  95. /**
  96. * Renders CSS
  97. *
  98. * @return string
  99. */
  100. public function renderCss()
  101. {
  102. return parent::renderCss() . ' col-price';
  103. }
  104. }