Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Price.php
Go to the documentation of this file.
1 <?php
8 
12 class Price extends \Magento\Framework\View\Element\Template implements \Magento\Framework\DataObject\IdentityInterface
13 {
17  protected $_priceDisplayType = null;
18 
22  protected $_idSuffix = '';
23 
29  protected $_coreRegistry = null;
30 
36  protected $_catalogData = null;
37 
41  protected $string;
42 
46  protected $mathRandom;
47 
51  protected $_jsonEncoder;
52 
56  protected $_cartHelper;
57 
68  public function __construct(
69  \Magento\Framework\View\Element\Template\Context $context,
70  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
71  \Magento\Catalog\Helper\Data $catalogData,
72  \Magento\Framework\Registry $registry,
73  \Magento\Framework\Stdlib\StringUtils $string,
74  \Magento\Framework\Math\Random $mathRandom,
75  \Magento\Checkout\Helper\Cart $cartHelper,
76  array $data = []
77  ) {
78  $this->_jsonEncoder = $jsonEncoder;
79  $this->_catalogData = $catalogData;
80  $this->_coreRegistry = $registry;
81  $this->string = $string;
82  $this->mathRandom = $mathRandom;
83  $this->_cartHelper = $cartHelper;
84  parent::__construct($context, $data);
85  }
86 
92  public function getProduct()
93  {
94  $product = $this->_getData('product');
95  if (!$product) {
96  $product = $this->_coreRegistry->registry('product');
97  }
98  return $product;
99  }
100 
104  public function getDisplayMinimalPrice()
105  {
106  return $this->_getData('display_minimal_price');
107  }
108 
113  public function setIdSuffix($idSuffix)
114  {
115  $this->_idSuffix = $idSuffix;
116  return $this;
117  }
118 
122  public function getIdSuffix()
123  {
124  return $this->_idSuffix;
125  }
126 
134  public function getAddToCartUrl($product, $additional = [])
135  {
136  return $this->_cartHelper->getAddUrl($product, $additional);
137  }
138 
144  protected function _toHtml()
145  {
146  if (!$this->getProduct() || $this->getProduct()->getCanShowPrice() === false) {
147  return '';
148  }
149  return parent::_toHtml();
150  }
151 
158  public function getRealPriceJs($product)
159  {
160  $html = $this->hasRealPriceHtml() ? $this->getRealPriceHtml() : $product->getRealPriceHtml();
161  return $this->_jsonEncoder->encode($html);
162  }
163 
170  public function prepareSku($sku)
171  {
172  return $this->escapeHtml($this->string->splitInjection($sku));
173  }
174 
182  public function getRandomString($length, $chars = null)
183  {
184  return $this->mathRandom->getRandomString($length, $chars);
185  }
186 
192  public function getIdentities()
193  {
194  return $this->getProduct()->getIdentities();
195  }
196 }
getRandomString($length, $chars=null)
Definition: Price.php:182
getAddToCartUrl($product, $additional=[])
Definition: Price.php:134
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Catalog\Helper\Data $catalogData, \Magento\Framework\Registry $registry, \Magento\Framework\Stdlib\StringUtils $string, \Magento\Framework\Math\Random $mathRandom, \Magento\Checkout\Helper\Cart $cartHelper, array $data=[])
Definition: Price.php:68
$idSuffix