Cart.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Block;
  7. use Magento\Customer\Model\Context;
  8. /**
  9. * Shopping cart block
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Cart extends \Magento\Checkout\Block\Cart\AbstractCart
  15. {
  16. /**
  17. * @var \Magento\Catalog\Model\ResourceModel\Url
  18. */
  19. protected $_catalogUrlBuilder;
  20. /**
  21. * @var \Magento\Framework\App\Http\Context
  22. */
  23. protected $httpContext;
  24. /**
  25. * @var \Magento\Checkout\Helper\Cart
  26. */
  27. protected $_cartHelper;
  28. /**
  29. * @param \Magento\Framework\View\Element\Template\Context $context
  30. * @param \Magento\Customer\Model\Session $customerSession
  31. * @param \Magento\Checkout\Model\Session $checkoutSession
  32. * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrlBuilder
  33. * @param \Magento\Checkout\Helper\Cart $cartHelper
  34. * @param \Magento\Framework\App\Http\Context $httpContext
  35. * @param array $data
  36. * @codeCoverageIgnore
  37. */
  38. public function __construct(
  39. \Magento\Framework\View\Element\Template\Context $context,
  40. \Magento\Customer\Model\Session $customerSession,
  41. \Magento\Checkout\Model\Session $checkoutSession,
  42. \Magento\Catalog\Model\ResourceModel\Url $catalogUrlBuilder,
  43. \Magento\Checkout\Helper\Cart $cartHelper,
  44. \Magento\Framework\App\Http\Context $httpContext,
  45. array $data = []
  46. ) {
  47. $this->_cartHelper = $cartHelper;
  48. $this->_catalogUrlBuilder = $catalogUrlBuilder;
  49. parent::__construct($context, $customerSession, $checkoutSession, $data);
  50. $this->_isScopePrivate = true;
  51. $this->httpContext = $httpContext;
  52. }
  53. /**
  54. * Prepare Quote Item Product URLs
  55. *
  56. * @codeCoverageIgnore
  57. * @return void
  58. */
  59. protected function _construct()
  60. {
  61. parent::_construct();
  62. $this->prepareItemUrls();
  63. }
  64. /**
  65. * prepare cart items URLs
  66. *
  67. * @return void
  68. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  69. */
  70. public function prepareItemUrls()
  71. {
  72. $products = [];
  73. /* @var $item \Magento\Quote\Model\Quote\Item */
  74. foreach ($this->getItems() as $item) {
  75. $product = $item->getProduct();
  76. $option = $item->getOptionByCode('product_type');
  77. if ($option) {
  78. $product = $option->getProduct();
  79. }
  80. if ($item->getStoreId() != $this->_storeManager->getStore()->getId() &&
  81. !$item->getRedirectUrl() &&
  82. !$product->isVisibleInSiteVisibility()
  83. ) {
  84. $products[$product->getId()] = $item->getStoreId();
  85. }
  86. }
  87. if ($products) {
  88. $products = $this->_catalogUrlBuilder->getRewriteByProductStore($products);
  89. foreach ($this->getItems() as $item) {
  90. $product = $item->getProduct();
  91. $option = $item->getOptionByCode('product_type');
  92. if ($option) {
  93. $product = $option->getProduct();
  94. }
  95. if (isset($products[$product->getId()])) {
  96. $object = new \Magento\Framework\DataObject($products[$product->getId()]);
  97. $item->getProduct()->setUrlDataObject($object);
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * @codeCoverageIgnore
  104. * @return bool
  105. */
  106. public function hasError()
  107. {
  108. return $this->getQuote()->getHasError();
  109. }
  110. /**
  111. * @codeCoverageIgnore
  112. * @return int
  113. */
  114. public function getItemsSummaryQty()
  115. {
  116. return $this->getQuote()->getItemsSummaryQty();
  117. }
  118. /**
  119. * @codeCoverageIgnore
  120. * @return bool
  121. */
  122. public function isWishlistActive()
  123. {
  124. $isActive = $this->_getData('is_wishlist_active');
  125. if ($isActive === null) {
  126. $isActive = $this->_scopeConfig->getValue(
  127. 'wishlist/general/active',
  128. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  129. ) && $this->httpContext->getValue(
  130. Context::CONTEXT_AUTH
  131. );
  132. $this->setIsWishlistActive($isActive);
  133. }
  134. return $isActive;
  135. }
  136. /**
  137. * @codeCoverageIgnore
  138. * @return string
  139. */
  140. public function getCheckoutUrl()
  141. {
  142. return $this->getUrl('checkout', ['_secure' => true]);
  143. }
  144. /**
  145. * @return string
  146. */
  147. public function getContinueShoppingUrl()
  148. {
  149. $url = $this->getData('continue_shopping_url');
  150. if ($url === null) {
  151. $url = $this->_checkoutSession->getContinueShoppingUrl(true);
  152. if (!$url) {
  153. $url = $this->_urlBuilder->getUrl();
  154. }
  155. $this->setData('continue_shopping_url', $url);
  156. }
  157. return $url;
  158. }
  159. /**
  160. * @return bool
  161. * @codeCoverageIgnore
  162. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  163. */
  164. public function getIsVirtual()
  165. {
  166. return $this->_cartHelper->getIsVirtualQuote();
  167. }
  168. /**
  169. * Return list of available checkout methods
  170. *
  171. * @param string $alias Container block alias in layout
  172. * @return array
  173. */
  174. public function getMethods($alias)
  175. {
  176. $childName = $this->getLayout()->getChildName($this->getNameInLayout(), $alias);
  177. if ($childName) {
  178. return $this->getLayout()->getChildNames($childName);
  179. }
  180. return [];
  181. }
  182. /**
  183. * Return HTML of checkout method (link, button etc.)
  184. *
  185. * @param string $name Block name in layout
  186. * @return string
  187. * @throws \Magento\Framework\Exception\LocalizedException
  188. */
  189. public function getMethodHtml($name)
  190. {
  191. $block = $this->getLayout()->getBlock($name);
  192. if (!$block) {
  193. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid method: %1', $name));
  194. }
  195. return $block->toHtml();
  196. }
  197. /**
  198. * Return customer quote items
  199. *
  200. * @return array
  201. */
  202. public function getItems()
  203. {
  204. if ($this->getCustomItems()) {
  205. return $this->getCustomItems();
  206. }
  207. return parent::getItems();
  208. }
  209. /**
  210. * @codeCoverageIgnore
  211. * @return int
  212. */
  213. public function getItemsCount()
  214. {
  215. return $this->getQuote()->getItemsCount();
  216. }
  217. /**
  218. * Render pagination HTML
  219. *
  220. * @return string
  221. * @since 100.1.7
  222. */
  223. public function getPagerHtml()
  224. {
  225. return $this->getChildHtml('pager');
  226. }
  227. }