PriceBox.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Model\Product;
  8. use Magento\Framework\Json\Helper\Data;
  9. use Magento\Framework\Math\Random;
  10. use Magento\Framework\Pricing\Price\PriceInterface;
  11. use Magento\Framework\Pricing\Render\PriceBox as PriceBoxRender;
  12. use Magento\Framework\Pricing\Render\RendererPool;
  13. use Magento\Framework\View\Element\Template\Context;
  14. /**
  15. * Default catalog price box render
  16. *
  17. * @method string getPriceElementIdPrefix()
  18. * @method string getIdSuffix()
  19. */
  20. class PriceBox extends PriceBoxRender
  21. {
  22. /**
  23. * @var \Magento\Framework\Json\Helper\Data
  24. */
  25. protected $jsonHelper;
  26. /**
  27. * @var \Magento\Framework\Math\Random
  28. */
  29. protected $mathRandom;
  30. /**
  31. * @param Context $context
  32. * @param Product $saleableItem
  33. * @param PriceInterface $price
  34. * @param RendererPool $rendererPool
  35. * @param Data $jsonHelper
  36. * @param Random $mathRandom
  37. * @param array $data
  38. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  39. */
  40. public function __construct(
  41. Context $context,
  42. Product $saleableItem,
  43. PriceInterface $price,
  44. RendererPool $rendererPool,
  45. Data $jsonHelper,
  46. Random $mathRandom,
  47. array $data = []
  48. ) {
  49. $this->jsonHelper = $jsonHelper;
  50. $this->mathRandom = $mathRandom;
  51. parent::__construct($context, $saleableItem, $price, $rendererPool);
  52. }
  53. /**
  54. * Encode the mixed $valueToEncode into the JSON format
  55. *
  56. * @param mixed $valueToEncode
  57. * @return string
  58. */
  59. public function jsonEncode($valueToEncode)
  60. {
  61. return $this->jsonHelper->jsonEncode($valueToEncode);
  62. }
  63. /**
  64. * Get random string
  65. *
  66. * @param int $length
  67. * @param string|null $chars
  68. * @return string
  69. */
  70. public function getRandomString($length, $chars = null)
  71. {
  72. return $this->mathRandom->getRandomString($length, $chars);
  73. }
  74. /**
  75. * Check if quantity can be displayed for tier price with msrp
  76. *
  77. * @param Product $product
  78. * @return bool
  79. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  80. */
  81. public function getCanDisplayQty(Product $product)
  82. {
  83. //TODO Refactor - change to const similar to Model\Product\Type\Grouped::TYPE_CODE
  84. if ($product->getTypeId() == 'grouped') {
  85. return false;
  86. }
  87. return true;
  88. }
  89. }