Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Renderer.php
Go to the documentation of this file.
1 <?php
7 
16 
30  \Magento\Framework\DataObject\IdentityInterface
31 {
35  protected $_checkoutSession;
36 
40  protected $_item;
41 
45  protected $_productUrl;
46 
52  protected $_strictQtyMode = true;
53 
59  protected $_ignoreProductUrl = false;
60 
66  protected $_productConfig = null;
67 
71  protected $_urlHelper;
72 
76  protected $messageManager;
77 
81  protected $imageBuilder;
82 
86  protected $priceCurrency;
87 
92 
96  private $messageInterpretationStrategy;
97 
99  private $itemResolver;
100 
116  public function __construct(
117  \Magento\Framework\View\Element\Template\Context $context,
118  \Magento\Catalog\Helper\Product\Configuration $productConfig,
119  \Magento\Checkout\Model\Session $checkoutSession,
120  \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder,
121  \Magento\Framework\Url\Helper\Data $urlHelper,
122  \Magento\Framework\Message\ManagerInterface $messageManager,
124  \Magento\Framework\Module\Manager $moduleManager,
125  InterpretationStrategyInterface $messageInterpretationStrategy,
126  array $data = [],
127  ItemResolverInterface $itemResolver = null
128  ) {
129  $this->priceCurrency = $priceCurrency;
130  $this->imageBuilder = $imageBuilder;
131  $this->_urlHelper = $urlHelper;
132  $this->_productConfig = $productConfig;
133  $this->_checkoutSession = $checkoutSession;
134  $this->messageManager = $messageManager;
135  parent::__construct($context, $data);
136  $this->_isScopePrivate = true;
137  $this->moduleManager = $moduleManager;
138  $this->messageInterpretationStrategy = $messageInterpretationStrategy;
139  $this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(ItemResolverInterface::class);
140  }
141 
149  public function setItem(AbstractItem $item)
150  {
151  $this->_item = $item;
152  return $this;
153  }
154 
161  public function getItem()
162  {
163  return $this->_item;
164  }
165 
172  public function getProduct()
173  {
174  return $this->getItem()->getProduct();
175  }
176 
183  public function getProductForThumbnail()
184  {
185  return $this->itemResolver->getFinalProduct($this->getItem());
186  }
187 
193  public function overrideProductUrl($productUrl)
194  {
195  $this->_productUrl = $productUrl;
196  return $this;
197  }
198 
204  public function hasProductUrl()
205  {
206  if ($this->_ignoreProductUrl) {
207  return false;
208  }
209 
210  if ($this->_productUrl || $this->getItem()->getRedirectUrl()) {
211  return true;
212  }
213 
214  $product = $this->getProduct();
215  $option = $this->getItem()->getOptionByCode('product_type');
216  if ($option) {
217  $product = $option->getProduct();
218  }
219 
220  if ($product->isVisibleInSiteVisibility()) {
221  return true;
222  } else {
223  if ($product->hasUrlDataObject()) {
224  $data = $product->getUrlDataObject();
225  if (in_array($data->getVisibility(), $product->getVisibleInSiteVisibilities())) {
226  return true;
227  }
228  }
229  }
230  return false;
231  }
232 
238  public function getProductUrl()
239  {
240  if ($this->_productUrl !== null) {
241  return $this->_productUrl;
242  }
243  if ($this->getItem()->getRedirectUrl()) {
244  return $this->getItem()->getRedirectUrl();
245  }
246 
247  $product = $this->getProduct();
248  $option = $this->getItem()->getOptionByCode('product_type');
249  if ($option) {
250  $product = $option->getProduct();
251  }
252 
253  return $product->getUrlModel()->getUrl($product);
254  }
255 
261  public function getProductName()
262  {
263  if ($this->hasProductName()) {
264  return $this->getData('product_name');
265  }
266  return $this->getProduct()->getName();
267  }
268 
274  public function getProductOptions()
275  {
276  /* @var $helper \Magento\Catalog\Helper\Product\Configuration */
278  return $helper->getCustomOptions($this->getItem());
279  }
280 
287  public function getOptionList()
288  {
289  return $this->getProductOptions();
290  }
291 
297  public function getQty()
298  {
299  if (!$this->_strictQtyMode && (string)$this->getItem()->getQty() == '') {
300  return '';
301  }
302  return $this->getItem()->getQty() * 1;
303  }
304 
311  public function getCheckoutSession()
312  {
314  }
315 
325  public function getMessages()
326  {
327  $messages = [];
328  $quoteItem = $this->getItem();
329 
330  // Add basic messages occurring during this page load
331  $baseMessages = $quoteItem->getMessage(false);
332  if ($baseMessages) {
333  foreach ($baseMessages as $message) {
334  $messages[] = ['text' => $message, 'type' => $quoteItem->getHasError() ? 'error' : 'notice'];
335  }
336  }
337 
338  /* @var $collection \Magento\Framework\Message\Collection */
339  $collection = $this->messageManager->getMessages(true, 'quote_item' . $quoteItem->getId());
340  if ($collection) {
341  $additionalMessages = $collection->getItems();
342  foreach ($additionalMessages as $message) {
343  /* @var $message \Magento\Framework\Message\MessageInterface */
344  $messages[] = [
345  'text' => $this->messageInterpretationStrategy->interpret($message),
346  'type' => $message->getType()
347  ];
348  }
349  }
350  $this->messageManager->getMessages(true, 'quote_item' . $quoteItem->getId())->clear();
351 
352  return $messages;
353  }
354 
376  {
377  /* @var $helper \Magento\Catalog\Helper\Product\Configuration */
379  $params = [
380  'max_length' => 55,
381  'cut_replacer' => ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>'
382  ];
383  return $helper->getFormattedOptionValue($optionValue, $params);
384  }
385 
392  public function isProductVisible()
393  {
394  return $this->getProduct()->isVisibleInSiteVisibility();
395  }
396 
404  {
405  return $this->getLayout()->getBlock('additional.product.info');
406  }
407 
415  public function setQtyMode($strict)
416  {
417  $this->_strictQtyMode = $strict;
418  return $this;
419  }
420 
428  public function setIgnoreProductUrl($ignore = true)
429  {
430  $this->_ignoreProductUrl = $ignore;
431  return $this;
432  }
433 
439  public function getIdentities()
440  {
441  $identities = [];
442  if ($this->getItem()) {
443  $identities = $this->getProduct()->getIdentities();
444  }
445  return $identities;
446  }
447 
454  public function getProductPriceHtml(\Magento\Catalog\Model\Product $product)
455  {
456  $priceRender = $this->getPriceRender();
457  $priceRender->setItem($this->getItem());
458 
459  $price = '';
460  if ($priceRender) {
461  $price = $priceRender->render(
462  ConfiguredPriceInterface::CONFIGURED_PRICE_CODE,
463  $product,
464  [
465  'include_container' => true,
466  'display_minimal_price' => true,
467  'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST
468  ]
469  );
470  }
471 
472  return $price;
473  }
474 
479  protected function getPriceRender()
480  {
481  return $this->getLayout()->getBlock('product.price.render.default');
482  }
483 
491  public function convertPrice($amount, $format = false)
492  {
493  return $format
494  ? $this->priceCurrency->convertAndFormat($amount)
495  : $this->priceCurrency->convert($amount);
496  }
497 
504  public function getUnitPriceHtml(AbstractItem $item)
505  {
507  $block = $this->getLayout()->getBlock('checkout.item.price.unit');
508  $block->setItem($item);
509  return $block->toHtml();
510  }
511 
518  public function getRowTotalHtml(AbstractItem $item)
519  {
521  $block = $this->getLayout()->getBlock('checkout.item.price.row');
522  $block->setItem($item);
523  return $block->toHtml();
524  }
525 
532  public function getSidebarItemPriceHtml(AbstractItem $item)
533  {
535  $block = $this->getLayout()->getBlock('checkout.cart.item.price.sidebar');
536  $block->setItem($item);
537  return $block->toHtml();
538  }
539 
546  public function getUnitPriceExclTaxHtml(AbstractItem $item)
547  {
549  $block = $this->getLayout()->getBlock('checkout.onepage.review.item.price.unit.excl');
550  $block->setItem($item);
551  return $block->toHtml();
552  }
553 
560  public function getUnitPriceInclTaxHtml(AbstractItem $item)
561  {
563  $block = $this->getLayout()->getBlock('checkout.onepage.review.item.price.unit.incl');
564  $block->setItem($item);
565  return $block->toHtml();
566  }
567 
574  public function getRowTotalExclTaxHtml(AbstractItem $item)
575  {
577  $block = $this->getLayout()->getBlock('checkout.onepage.review.item.price.rowtotal.excl');
578  $block->setItem($item);
579  return $block->toHtml();
580  }
581 
588  public function getRowTotalInclTaxHtml(AbstractItem $item)
589  {
591  $block = $this->getLayout()->getBlock('checkout.onepage.review.item.price.rowtotal.incl');
592  $block->setItem($item);
593  return $block->toHtml();
594  }
595 
602  public function getActions(AbstractItem $item)
603  {
605  $block = $this->getChildBlock('actions');
606  if ($block instanceof Actions) {
607  $block->setItem($item);
608  return $block->toHtml();
609  } else {
610  return '';
611  }
612  }
613 
622  public function getImage($product, $imageId, $attributes = [])
623  {
624  return $this->imageBuilder->create($product, $imageId, $attributes);
625  }
626 }
$helper
Definition: iframe.phtml:13
getImage($product, $imageId, $attributes=[])
Definition: Renderer.php:622
getData($key='', $index=null)
Definition: DataObject.php:119
$price
$message
$block
Definition: block.php:8
$amount
Definition: order.php:14
$format
Definition: list.phtml:12
$quoteItem
Definition: quote.php:38
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Helper\Product\Configuration $productConfig, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder, \Magento\Framework\Url\Helper\Data $urlHelper, \Magento\Framework\Message\ManagerInterface $messageManager, PriceCurrencyInterface $priceCurrency, \Magento\Framework\Module\Manager $moduleManager, InterpretationStrategyInterface $messageInterpretationStrategy, array $data=[], ItemResolverInterface $itemResolver=null)
Definition: Renderer.php:116
$attributes
Definition: matrix.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
convertPrice($amount, $format=false)
Definition: Renderer.php:491
getProductPriceHtml(\Magento\Catalog\Model\Product $product)
Definition: Renderer.php:454