Skip to content

Commit 2e53cf0

Browse files
author
Alexander Akimov
authored
Merge pull request #819 from magento-folks/cart_optimization
[Folks] Cart Optimization
2 parents 2935aae + 0a393c5 commit 2e53cf0

File tree

82 files changed

+4524
-298
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+4524
-298
lines changed

app/code/Magento/Checkout/Block/Cart.php

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

app/code/Magento/Checkout/Block/Cart/Sidebar.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public function getConfig()
7070
'imageTemplate' => $this->getImageHtmlTemplate(),
7171
'baseUrl' => $this->getBaseUrl(),
7272
'minicartMaxItemsVisible' => $this->getMiniCartMaxItemsCount(),
73-
'websiteId' => $this->_storeManager->getStore()->getWebsiteId()
73+
'websiteId' => $this->_storeManager->getStore()->getWebsiteId(),
74+
'maxItemsToDisplay' => $this->getMaxItemsToDisplay()
7475
];
7576
}
7677

@@ -188,4 +189,18 @@ private function getMiniCartMaxItemsCount()
188189
{
189190
return (int)$this->_scopeConfig->getValue('checkout/sidebar/count', ScopeInterface::SCOPE_STORE);
190191
}
192+
193+
/**
194+
* Returns maximum cart items to display
195+
* This setting regulates how many items will be displayed in minicart
196+
*
197+
* @return int
198+
*/
199+
private function getMaxItemsToDisplay()
200+
{
201+
return (int)$this->_scopeConfig->getValue(
202+
'checkout/sidebar/max_items_display_count',
203+
ScopeInterface::SCOPE_STORE
204+
);
205+
}
191206
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Checkout\Model\Cart;
8+
9+
use Magento\Checkout\Model\ConfigProviderInterface;
10+
use Magento\Framework\UrlInterface;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
14+
/**
15+
* Class CheckoutSummaryConfigProvider provides configuration for checkout summary block
16+
*/
17+
class CheckoutSummaryConfigProvider implements ConfigProviderInterface
18+
{
19+
/**
20+
* @var UrlInterface
21+
*/
22+
private $urlBuilder;
23+
24+
/**
25+
* @var ScopeConfigInterface
26+
*/
27+
private $scopeConfig;
28+
29+
/**
30+
* @param UrlInterface $urlBuilder
31+
* @param ScopeConfigInterface $scopeConfig
32+
*/
33+
public function __construct(
34+
UrlInterface $urlBuilder,
35+
ScopeConfigInterface $scopeConfig
36+
) {
37+
$this->urlBuilder = $urlBuilder;
38+
$this->scopeConfig = $scopeConfig;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getConfig()
45+
{
46+
return [
47+
'maxCartItemsToDisplay' => $this->getMaxCartItemsToDisplay(),
48+
'cartUrl' => $this->urlBuilder->getUrl('checkout/cart')
49+
];
50+
}
51+
52+
/**
53+
* Returns maximum cart items to display
54+
* This setting regulates how many items will be displayed in checkout summary block
55+
*
56+
* @return int
57+
*/
58+
private function getMaxCartItemsToDisplay()
59+
{
60+
return (int)$this->scopeConfig->getValue(
61+
'checkout/options/max_items_display_count',
62+
ScopeInterface::SCOPE_STORE
63+
);
64+
}
65+
}

0 commit comments

Comments
 (0)