Cart.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\CustomerData;
  7. use Magento\Customer\CustomerData\SectionSourceInterface;
  8. /**
  9. * Cart source
  10. *
  11. * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
  12. */
  13. class Cart extends \Magento\Framework\DataObject implements SectionSourceInterface
  14. {
  15. /**
  16. * @var \Magento\Customer\Model\Session
  17. */
  18. protected $checkoutSession;
  19. /**
  20. * @var \Magento\Checkout\Model\Cart
  21. */
  22. protected $checkoutCart;
  23. /**
  24. * @var \Magento\Catalog\Model\ResourceModel\Url
  25. */
  26. protected $catalogUrl;
  27. /**
  28. * @var \Magento\Quote\Model\Quote|null
  29. */
  30. protected $quote = null;
  31. /**
  32. * @var \Magento\Checkout\Helper\Data
  33. */
  34. protected $checkoutHelper;
  35. /**
  36. * @var ItemPoolInterface
  37. */
  38. protected $itemPoolInterface;
  39. /**
  40. * @var int|float
  41. */
  42. protected $summeryCount;
  43. /**
  44. * @var \Magento\Framework\View\LayoutInterface
  45. */
  46. protected $layout;
  47. /**
  48. * @param \Magento\Checkout\Model\Session $checkoutSession
  49. * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrl
  50. * @param \Magento\Checkout\Model\Cart $checkoutCart
  51. * @param \Magento\Checkout\Helper\Data $checkoutHelper
  52. * @param ItemPoolInterface $itemPoolInterface
  53. * @param \Magento\Framework\View\LayoutInterface $layout
  54. * @param array $data
  55. * @codeCoverageIgnore
  56. */
  57. public function __construct(
  58. \Magento\Checkout\Model\Session $checkoutSession,
  59. \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
  60. \Magento\Checkout\Model\Cart $checkoutCart,
  61. \Magento\Checkout\Helper\Data $checkoutHelper,
  62. ItemPoolInterface $itemPoolInterface,
  63. \Magento\Framework\View\LayoutInterface $layout,
  64. array $data = []
  65. ) {
  66. parent::__construct($data);
  67. $this->checkoutSession = $checkoutSession;
  68. $this->catalogUrl = $catalogUrl;
  69. $this->checkoutCart = $checkoutCart;
  70. $this->checkoutHelper = $checkoutHelper;
  71. $this->itemPoolInterface = $itemPoolInterface;
  72. $this->layout = $layout;
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function getSectionData()
  78. {
  79. $totals = $this->getQuote()->getTotals();
  80. $subtotalAmount = $totals['subtotal']->getValue();
  81. return [
  82. 'summary_count' => $this->getSummaryCount(),
  83. 'subtotalAmount' => $subtotalAmount,
  84. 'subtotal' => isset($totals['subtotal'])
  85. ? $this->checkoutHelper->formatPrice($subtotalAmount)
  86. : 0,
  87. 'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
  88. 'items' => $this->getRecentItems(),
  89. 'extra_actions' => $this->layout->createBlock(\Magento\Catalog\Block\ShortcutButtons::class)->toHtml(),
  90. 'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
  91. 'website_id' => $this->getQuote()->getStore()->getWebsiteId(),
  92. 'storeId' => $this->getQuote()->getStore()->getStoreId()
  93. ];
  94. }
  95. /**
  96. * Get active quote
  97. *
  98. * @return \Magento\Quote\Model\Quote
  99. */
  100. protected function getQuote()
  101. {
  102. if (null === $this->quote) {
  103. $this->quote = $this->checkoutSession->getQuote();
  104. }
  105. return $this->quote;
  106. }
  107. /**
  108. * Get shopping cart items qty based on configuration (summary qty or items qty)
  109. *
  110. * @return int|float
  111. */
  112. protected function getSummaryCount()
  113. {
  114. if (!$this->summeryCount) {
  115. $this->summeryCount = $this->checkoutCart->getSummaryQty() ?: 0;
  116. }
  117. return $this->summeryCount;
  118. }
  119. /**
  120. * Check if one page checkout is available
  121. *
  122. * @return bool
  123. */
  124. protected function isPossibleOnepageCheckout()
  125. {
  126. return $this->checkoutHelper->canOnepageCheckout() && !$this->getQuote()->getHasError();
  127. }
  128. /**
  129. * Get array of last added items
  130. *
  131. * @return \Magento\Quote\Model\Quote\Item[]
  132. */
  133. protected function getRecentItems()
  134. {
  135. $items = [];
  136. if (!$this->getSummaryCount()) {
  137. return $items;
  138. }
  139. foreach (array_reverse($this->getAllQuoteItems()) as $item) {
  140. /* @var $item \Magento\Quote\Model\Quote\Item */
  141. if (!$item->getProduct()->isVisibleInSiteVisibility()) {
  142. $product = $item->getOptionByCode('product_type') !== null
  143. ? $item->getOptionByCode('product_type')->getProduct()
  144. : $item->getProduct();
  145. $products = $this->catalogUrl->getRewriteByProductStore([$product->getId() => $item->getStoreId()]);
  146. if (isset($products[$product->getId()])) {
  147. $urlDataObject = new \Magento\Framework\DataObject($products[$product->getId()]);
  148. $item->getProduct()->setUrlDataObject($urlDataObject);
  149. }
  150. }
  151. $items[] = $this->itemPoolInterface->getItemData($item);
  152. }
  153. return $items;
  154. }
  155. /**
  156. * Return customer quote items
  157. *
  158. * @return \Magento\Quote\Model\Quote\Item[]
  159. */
  160. protected function getAllQuoteItems()
  161. {
  162. if ($this->getCustomQuote()) {
  163. return $this->getCustomQuote()->getAllVisibleItems();
  164. }
  165. return $this->getQuote()->getAllVisibleItems();
  166. }
  167. /**
  168. * Check if guest checkout is allowed
  169. *
  170. * @return bool
  171. */
  172. public function isGuestCheckoutAllowed()
  173. {
  174. return $this->checkoutHelper->isAllowedGuestCheckout($this->checkoutSession->getQuote());
  175. }
  176. }