AbstractCart.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Block\Cart;
  7. use Magento\Quote\Model\Quote;
  8. /**
  9. * Shopping cart abstract block
  10. */
  11. class AbstractCart extends \Magento\Framework\View\Element\Template
  12. {
  13. /**
  14. * Block alias fallback
  15. */
  16. const DEFAULT_TYPE = 'default';
  17. /**
  18. * @var Quote|null
  19. */
  20. protected $_quote = null;
  21. /**
  22. * @var array
  23. */
  24. protected $_totals;
  25. /**
  26. * @var array
  27. */
  28. protected $_itemRenders = [];
  29. /**
  30. * TODO: MAGETWO-34827: unused object?
  31. *
  32. * @var \Magento\Customer\Model\Session
  33. */
  34. protected $_customerSession;
  35. /**
  36. * @var \Magento\Checkout\Model\Session
  37. */
  38. protected $_checkoutSession;
  39. /**
  40. * @param \Magento\Framework\View\Element\Template\Context $context
  41. * @param \Magento\Customer\Model\Session $customerSession
  42. * @param \Magento\Checkout\Model\Session $checkoutSession
  43. * @param array $data
  44. * @codeCoverageIgnore
  45. */
  46. public function __construct(
  47. \Magento\Framework\View\Element\Template\Context $context,
  48. \Magento\Customer\Model\Session $customerSession,
  49. \Magento\Checkout\Model\Session $checkoutSession,
  50. array $data = []
  51. ) {
  52. $this->_customerSession = $customerSession;
  53. $this->_checkoutSession = $checkoutSession;
  54. parent::__construct($context, $data);
  55. $this->_isScopePrivate = true;
  56. }
  57. /**
  58. * Retrieve renderer list
  59. *
  60. * @return \Magento\Framework\View\Element\RendererList
  61. */
  62. protected function _getRendererList()
  63. {
  64. return $this->getRendererListName() ? $this->getLayout()->getBlock(
  65. $this->getRendererListName()
  66. ) : $this->getChildBlock(
  67. 'renderer.list'
  68. );
  69. }
  70. /**
  71. * Retrieve item renderer block
  72. *
  73. * @param string|null $type
  74. * @return \Magento\Framework\View\Element\Template
  75. * @throws \RuntimeException
  76. */
  77. public function getItemRenderer($type = null)
  78. {
  79. if ($type === null) {
  80. $type = self::DEFAULT_TYPE;
  81. }
  82. $rendererList = $this->_getRendererList();
  83. if (!$rendererList) {
  84. throw new \RuntimeException('Renderer list for block "' . $this->getNameInLayout() . '" is not defined');
  85. }
  86. $overriddenTemplates = $this->getOverriddenTemplates() ?: [];
  87. $template = isset($overriddenTemplates[$type]) ? $overriddenTemplates[$type] : $this->getRendererTemplate();
  88. return $rendererList->getRenderer($type, self::DEFAULT_TYPE, $template);
  89. }
  90. /**
  91. * Get active quote
  92. *
  93. * @return Quote
  94. */
  95. public function getQuote()
  96. {
  97. if (null === $this->_quote) {
  98. $this->_quote = $this->_checkoutSession->getQuote();
  99. }
  100. return $this->_quote;
  101. }
  102. /**
  103. * Get all cart items
  104. *
  105. * @return array
  106. * @codeCoverageIgnore
  107. */
  108. public function getItems()
  109. {
  110. return $this->getQuote()->getAllVisibleItems();
  111. }
  112. /**
  113. * Get item row html
  114. *
  115. * @param \Magento\Quote\Model\Quote\Item $item
  116. * @return string
  117. */
  118. public function getItemHtml(\Magento\Quote\Model\Quote\Item $item)
  119. {
  120. $renderer = $this->getItemRenderer($item->getProductType())->setItem($item);
  121. return $renderer->toHtml();
  122. }
  123. /**
  124. * @return array
  125. * @codeCoverageIgnore
  126. */
  127. public function getTotals()
  128. {
  129. return $this->getTotalsCache();
  130. }
  131. /**
  132. * @return array
  133. */
  134. public function getTotalsCache()
  135. {
  136. if (empty($this->_totals)) {
  137. if ($this->getQuote()->isVirtual()) {
  138. $this->_totals = $this->getQuote()->getBillingAddress()->getTotals();
  139. } else {
  140. $this->_totals = $this->getQuote()->getShippingAddress()->getTotals();
  141. }
  142. }
  143. return $this->_totals;
  144. }
  145. }