Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PriceBox.php
Go to the documentation of this file.
1 <?php
8 
14 
22 {
24  const DEFAULT_LIFETIME = 3600;
25 
29  protected $saleableItem;
30 
34  protected $price;
35 
39  protected $rendererPool;
40 
48  public function __construct(
49  Template\Context $context,
53  array $data = []
54  ) {
55  $this->saleableItem = $saleableItem;
56  $this->price = $price;
57  $this->rendererPool = $rendererPool;
58  parent::__construct($context, $data);
59  }
60 
64  protected function _toHtml()
65  {
66  $cssClasses = $this->hasData('css_classes') ? explode(' ', $this->getData('css_classes')) : [];
67  $cssClasses[] = 'price-' . $this->getPrice()->getPriceCode();
68  $this->setData('css_classes', implode(' ', $cssClasses));
69  return parent::_toHtml();
70  }
71 
77  public function getCacheKey()
78  {
79  return parent::getCacheKey() . '-' . $this->getPriceId() . '-' . $this->getPrice()->getPriceCode();
80  }
81 
87  protected function getCacheLifetime()
88  {
89  return parent::hasCacheLifetime() ? parent::getCacheLifetime() : null;
90  }
91 
95  public function getSaleableItem()
96  {
97  return $this->saleableItem;
98  }
99 
103  public function getPrice()
104  {
105  return $this->price;
106  }
107 
115  public function getPriceId($defaultPrefix = null, $defaultSuffix = null)
116  {
117  if ($this->hasData('price_id')) {
118  return $this->getData('price_id');
119  }
120  $priceId = $this->saleableItem->getId();
121  $prefix = $this->hasData('price_id_prefix') ? $this->getData('price_id_prefix') : $defaultPrefix;
122  $suffix = $this->hasData('price_id_suffix') ? $this->getData('price_id_suffix') : $defaultSuffix;
123  $priceId = $prefix . $priceId . $suffix;
124  return $priceId;
125  }
126 
133  public function getPriceType($priceCode)
134  {
135  return $this->saleableItem->getPriceInfo()->getPrice($priceCode);
136  }
137 
143  public function renderAmount(AmountInterface $amount, array $arguments = [])
144  {
145  $arguments = array_replace($this->getData(), $arguments);
146 
147  //@TODO AmountInterface does not contain toHtml() method
148  return $this->getAmountRender($amount, $arguments)->toHtml();
149  }
150 
156  protected function getAmountRender(AmountInterface $amount, array $arguments = [])
157  {
158  return $this->rendererPool->createAmountRender(
159  $amount,
160  $this->getSaleableItem(),
161  $this->getPrice(),
162  $arguments
163  );
164  }
165 
169  public function getRendererPool()
170  {
171  return $this->rendererPool;
172  }
173 
179  public function getIdentities()
180  {
181  $item = $this->getSaleableItem();
182  if ($item instanceof IdentityInterface) {
183  return $item->getIdentities();
184  } else {
185  return [];
186  }
187  }
188 }
$suffix
Definition: name.phtml:27
getData($key='', $index=null)
Definition: DataObject.php:119
__construct(Template\Context $context, SaleableInterface $saleableItem, PriceInterface $price, RendererPool $rendererPool, array $data=[])
Definition: PriceBox.php:48
$amount
Definition: order.php:14
$prefix
Definition: name.phtml:25
getPriceId($defaultPrefix=null, $defaultSuffix=null)
Definition: PriceBox.php:115
getAmountRender(AmountInterface $amount, array $arguments=[])
Definition: PriceBox.php:156
$arguments
setData($key, $value=null)
Definition: DataObject.php:72
renderAmount(AmountInterface $amount, array $arguments=[])
Definition: PriceBox.php:143