Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions app/code/Magento/Checkout/Block/Cart/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

namespace Magento\Checkout\Block\Cart;

use Magento\Quote\Model\ResourceModel\Quote\Item\Collection;
use Magento\Store\Model\ScopeInterface;
use Magento\Theme\Block\Html\Pager;

/**
* Block on checkout/cart/index page to display a pager on the cart items grid
* The pager will be displayed if items quantity in the shopping cart > than number from
Expand All @@ -23,7 +27,7 @@ class Grid extends \Magento\Checkout\Block\Cart
const XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER = 'checkout/cart/number_items_to_display_pager';

/**
* @var \Magento\Quote\Model\ResourceModel\Quote\Item\Collection
* @var Collection
*/
private $itemsCollection;

Expand Down Expand Up @@ -110,29 +114,53 @@ protected function _prepareLayout()
if ($this->isPagerDisplayedOnPage()) {
$availableLimit = (int)$this->_scopeConfig->getValue(
self::XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
ScopeInterface::SCOPE_STORE
);
$itemsCollection = $this->getItemsForGrid();
/** @var \Magento\Theme\Block\Html\Pager $pager */
$pager = $this->getLayout()->createBlock(\Magento\Theme\Block\Html\Pager::class);
/** @var Pager $pager */
$pager = $this->getLayout()->createBlock(Pager::class);
$pager->setAvailableLimit([$availableLimit => $availableLimit])->setCollection($itemsCollection);
$this->setChild('pager', $pager);
$this->updateCollectionWithQuoteItems($itemsCollection);
$itemsCollection->load();
$this->prepareItemUrls();
}
return $this;
}

private function updateCollectionWithQuoteItems(
Collection $itemsCollection
) {
foreach ($itemsCollection->getItems() as $key => $collectionItem) {
$quote = $this->getQuote();
$quoteItems = $quote->getItems();
foreach ($quoteItems as $quoteItem) {
if ($quoteItem->getSku() === $collectionItem->getSku()) {
$itemsCollection->removeItemByKey($key);
try {
$itemsCollection->addItem($quoteItem);
} catch (\Exception $e) {
// No action, item id removed
}

break;
}
}
}

return $itemsCollection;
}

/**
* Prepare quote items collection for pager
*
* @return \Magento\Quote\Model\ResourceModel\Quote\Item\Collection
* @return Collection
* @since 100.1.7
*/
public function getItemsForGrid()
{
if (!$this->itemsCollection) {
/** @var \Magento\Quote\Model\ResourceModel\Quote\Item\Collection $itemCollection */
/** @var Collection $itemCollection */
$itemCollection = $this->itemCollectionFactory->create();

$itemCollection->setQuote($this->getQuote());
Expand Down Expand Up @@ -167,7 +195,7 @@ private function isPagerDisplayedOnPage()
if (!$this->isPagerDisplayed) {
$availableLimit = (int)$this->_scopeConfig->getValue(
self::XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
ScopeInterface::SCOPE_STORE
);
$this->isPagerDisplayed = !$this->getCustomItems() && $availableLimit < $this->getItemsCount();
}
Expand Down