Grid.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  8. * Block on checkout/cart/index page to display a pager on the cart items grid
  9. * The pager will be displayed if items quantity in the shopping cart > than number from
  10. * Store->Configuration->Sales->Checkout->Shopping Cart->Number of items to display pager and
  11. * custom_items weren't set to cart block
  12. *
  13. * @api
  14. * @since 100.1.7
  15. */
  16. class Grid extends \Magento\Checkout\Block\Cart
  17. {
  18. /**
  19. * Config settings path to determine when pager on checkout/cart/index will be visible
  20. */
  21. const XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER = 'checkout/cart/number_items_to_display_pager';
  22. /**
  23. * @var \Magento\Quote\Model\ResourceModel\Quote\Item\Collection
  24. */
  25. private $itemsCollection;
  26. /**
  27. * @var \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory
  28. *
  29. */
  30. private $itemCollectionFactory;
  31. /**
  32. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
  33. */
  34. private $joinAttributeProcessor;
  35. /**
  36. * Is display pager on shopping cart page
  37. *
  38. * @var bool
  39. */
  40. private $isPagerDisplayed;
  41. /**
  42. * Grid constructor.
  43. * @param \Magento\Framework\View\Element\Template\Context $context
  44. * @param \Magento\Customer\Model\Session $customerSession
  45. * @param \Magento\Checkout\Model\Session $checkoutSession
  46. * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrlBuilder
  47. * @param \Magento\Checkout\Helper\Cart $cartHelper
  48. * @param \Magento\Framework\App\Http\Context $httpContext
  49. * @param \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory $itemCollectionFactory
  50. * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor
  51. * @param array $data
  52. */
  53. public function __construct(
  54. \Magento\Framework\View\Element\Template\Context $context,
  55. \Magento\Customer\Model\Session $customerSession,
  56. \Magento\Checkout\Model\Session $checkoutSession,
  57. \Magento\Catalog\Model\ResourceModel\Url $catalogUrlBuilder,
  58. \Magento\Checkout\Helper\Cart $cartHelper,
  59. \Magento\Framework\App\Http\Context $httpContext,
  60. \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory $itemCollectionFactory,
  61. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
  62. array $data = []
  63. ) {
  64. $this->itemCollectionFactory = $itemCollectionFactory;
  65. $this->joinAttributeProcessor = $joinProcessor;
  66. parent::__construct(
  67. $context,
  68. $customerSession,
  69. $checkoutSession,
  70. $catalogUrlBuilder,
  71. $cartHelper,
  72. $httpContext,
  73. $data
  74. );
  75. }
  76. /**
  77. * Prepare Quote Item Product URLs
  78. * When we don't have custom_items, items URLs will be collected for Collection limited by pager
  79. * Pager limit on checkout/cart/index is determined by configuration
  80. * Configuration path is Store->Configuration->Sales->Checkout->Shopping Cart->Number of items to display pager
  81. *
  82. * @return void
  83. * @since 100.1.7
  84. */
  85. protected function _construct()
  86. {
  87. if (!$this->isPagerDisplayedOnPage()) {
  88. parent::_construct();
  89. }
  90. if ($this->hasData('template')) {
  91. $this->setTemplate($this->getData('template'));
  92. }
  93. }
  94. /**
  95. * {@inheritdoc}
  96. * @since 100.1.7
  97. */
  98. protected function _prepareLayout()
  99. {
  100. parent::_prepareLayout();
  101. if ($this->isPagerDisplayedOnPage()) {
  102. $availableLimit = (int)$this->_scopeConfig->getValue(
  103. self::XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER,
  104. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  105. );
  106. $itemsCollection = $this->getItemsForGrid();
  107. /** @var \Magento\Theme\Block\Html\Pager $pager */
  108. $pager = $this->getLayout()->createBlock(\Magento\Theme\Block\Html\Pager::class);
  109. $pager->setAvailableLimit([$availableLimit => $availableLimit])->setCollection($itemsCollection);
  110. $this->setChild('pager', $pager);
  111. $itemsCollection->load();
  112. $this->prepareItemUrls();
  113. }
  114. return $this;
  115. }
  116. /**
  117. * Prepare quote items collection for pager
  118. *
  119. * @return \Magento\Quote\Model\ResourceModel\Quote\Item\Collection
  120. * @since 100.1.7
  121. */
  122. public function getItemsForGrid()
  123. {
  124. if (!$this->itemsCollection) {
  125. /** @var \Magento\Quote\Model\ResourceModel\Quote\Item\Collection $itemCollection */
  126. $itemCollection = $this->itemCollectionFactory->create();
  127. $itemCollection->setQuote($this->getQuote());
  128. $itemCollection->addFieldToFilter('parent_item_id', ['null' => true]);
  129. $this->joinAttributeProcessor->process($itemCollection);
  130. $this->itemsCollection = $itemCollection;
  131. }
  132. return $this->itemsCollection;
  133. }
  134. /**
  135. * {@inheritdoc}
  136. * @since 100.1.7
  137. */
  138. public function getItems()
  139. {
  140. if (!$this->isPagerDisplayedOnPage()) {
  141. return parent::getItems();
  142. }
  143. return $this->getItemsForGrid()->getItems();
  144. }
  145. /**
  146. * Verify if display pager on shopping cart
  147. * If cart block has custom_items and items qty in the shopping cart<limit from stores configuration
  148. *
  149. * @return bool
  150. */
  151. private function isPagerDisplayedOnPage()
  152. {
  153. if (!$this->isPagerDisplayed) {
  154. $availableLimit = (int)$this->_scopeConfig->getValue(
  155. self::XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER,
  156. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  157. );
  158. $this->isPagerDisplayed = !$this->getCustomItems() && $availableLimit < $this->getItemsCount();
  159. }
  160. return $this->isPagerDisplayed;
  161. }
  162. }