Sidebar.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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\Store\Model\ScopeInterface;
  8. /**
  9. * Cart sidebar block
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Sidebar extends AbstractCart
  15. {
  16. /**
  17. * Xml pah to checkout sidebar display value
  18. */
  19. const XML_PATH_CHECKOUT_SIDEBAR_DISPLAY = 'checkout/sidebar/display';
  20. /**
  21. * Xml pah to checkout sidebar count value
  22. */
  23. const XML_PATH_CHECKOUT_SIDEBAR_COUNT = 'checkout/sidebar/count';
  24. /**
  25. * @var \Magento\Catalog\Helper\Image
  26. */
  27. protected $imageHelper;
  28. /**
  29. * @var \Magento\Framework\Serialize\Serializer\Json
  30. */
  31. private $serializer;
  32. /**
  33. * @param \Magento\Framework\View\Element\Template\Context $context
  34. * @param \Magento\Customer\Model\Session $customerSession
  35. * @param \Magento\Checkout\Model\Session $checkoutSession
  36. * @param \Magento\Catalog\Helper\Image $imageHelper
  37. * @param \Magento\Customer\CustomerData\JsLayoutDataProviderPoolInterface $jsLayoutDataProvider
  38. * @param array $data
  39. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  40. * @throws \RuntimeException
  41. */
  42. public function __construct(
  43. \Magento\Framework\View\Element\Template\Context $context,
  44. \Magento\Customer\Model\Session $customerSession,
  45. \Magento\Checkout\Model\Session $checkoutSession,
  46. \Magento\Catalog\Helper\Image $imageHelper,
  47. \Magento\Customer\CustomerData\JsLayoutDataProviderPoolInterface $jsLayoutDataProvider,
  48. array $data = [],
  49. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  50. ) {
  51. if (isset($data['jsLayout'])) {
  52. $this->jsLayout = array_merge_recursive($jsLayoutDataProvider->getData(), $data['jsLayout']);
  53. unset($data['jsLayout']);
  54. } else {
  55. $this->jsLayout = $jsLayoutDataProvider->getData();
  56. }
  57. parent::__construct($context, $customerSession, $checkoutSession, $data);
  58. $this->_isScopePrivate = false;
  59. $this->imageHelper = $imageHelper;
  60. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  61. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  62. }
  63. /**
  64. * Returns minicart config
  65. *
  66. * @return array
  67. */
  68. public function getConfig()
  69. {
  70. return [
  71. 'shoppingCartUrl' => $this->getShoppingCartUrl(),
  72. 'checkoutUrl' => $this->getCheckoutUrl(),
  73. 'updateItemQtyUrl' => $this->getUpdateItemQtyUrl(),
  74. 'removeItemUrl' => $this->getRemoveItemUrl(),
  75. 'imageTemplate' => $this->getImageHtmlTemplate(),
  76. 'baseUrl' => $this->getBaseUrl(),
  77. 'minicartMaxItemsVisible' => $this->getMiniCartMaxItemsCount(),
  78. 'websiteId' => $this->_storeManager->getStore()->getWebsiteId(),
  79. 'maxItemsToDisplay' => $this->getMaxItemsToDisplay(),
  80. 'storeId' => $this->_storeManager->getStore()->getId()
  81. ];
  82. }
  83. /**
  84. * Get serialized config
  85. *
  86. * @return string
  87. * @since 100.2.0
  88. */
  89. public function getSerializedConfig()
  90. {
  91. return $this->serializer->serialize($this->getConfig());
  92. }
  93. /**
  94. * Get image html template
  95. *
  96. * @return string
  97. */
  98. public function getImageHtmlTemplate()
  99. {
  100. return 'Magento_Catalog/product/image_with_borders';
  101. }
  102. /**
  103. * Get one page checkout page url
  104. *
  105. * @codeCoverageIgnore
  106. * @return string
  107. */
  108. public function getCheckoutUrl()
  109. {
  110. return $this->getUrl('checkout');
  111. }
  112. /**
  113. * Get shopping cart page url
  114. *
  115. * @return string
  116. * @codeCoverageIgnore
  117. */
  118. public function getShoppingCartUrl()
  119. {
  120. return $this->getUrl('checkout/cart');
  121. }
  122. /**
  123. * Get update cart item url
  124. *
  125. * @return string
  126. * @codeCoverageIgnore
  127. * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
  128. */
  129. public function getUpdateItemQtyUrl()
  130. {
  131. return $this->getUrl('checkout/sidebar/updateItemQty', ['_secure' => $this->getRequest()->isSecure()]);
  132. }
  133. /**
  134. * Get remove cart item url
  135. *
  136. * @return string
  137. * @codeCoverageIgnore
  138. * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
  139. */
  140. public function getRemoveItemUrl()
  141. {
  142. return $this->getUrl('checkout/sidebar/removeItem', ['_secure' => $this->getRequest()->isSecure()]);
  143. }
  144. /**
  145. * Define if Mini Shopping Cart Pop-Up Menu enabled
  146. *
  147. * @return bool
  148. * @codeCoverageIgnore
  149. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  150. */
  151. public function getIsNeedToDisplaySideBar()
  152. {
  153. return (bool)$this->_scopeConfig->getValue(
  154. self::XML_PATH_CHECKOUT_SIDEBAR_DISPLAY,
  155. ScopeInterface::SCOPE_STORE
  156. );
  157. }
  158. /**
  159. * Return totals from custom quote if needed
  160. *
  161. * @return array
  162. */
  163. public function getTotalsCache()
  164. {
  165. if (empty($this->_totals)) {
  166. $quote = $this->getCustomQuote() ? $this->getCustomQuote() : $this->getQuote();
  167. $this->_totals = $quote->getTotals();
  168. }
  169. return $this->_totals;
  170. }
  171. /**
  172. * Retrieve subtotal block html
  173. *
  174. * @codeCoverageIgnore
  175. * @return string
  176. */
  177. public function getTotalsHtml()
  178. {
  179. return $this->getLayout()->getBlock('checkout.cart.minicart.totals')->toHtml();
  180. }
  181. /**
  182. * Return base url.
  183. *
  184. * @codeCoverageIgnore
  185. * @return string
  186. */
  187. public function getBaseUrl()
  188. {
  189. return $this->_storeManager->getStore()->getBaseUrl();
  190. }
  191. /**
  192. * Return max visible item count for minicart
  193. *
  194. * @return int
  195. */
  196. private function getMiniCartMaxItemsCount()
  197. {
  198. return (int)$this->_scopeConfig->getValue('checkout/sidebar/count', ScopeInterface::SCOPE_STORE);
  199. }
  200. /**
  201. * Returns maximum cart items to display
  202. *
  203. * This setting regulates how many items will be displayed in minicart
  204. *
  205. * @return int
  206. */
  207. private function getMaxItemsToDisplay()
  208. {
  209. return (int)$this->_scopeConfig->getValue(
  210. 'checkout/sidebar/max_items_display_count',
  211. ScopeInterface::SCOPE_STORE
  212. );
  213. }
  214. }