Skip to content

Commit 4c6c053

Browse files
committed
ACPT-677: Catalog product grid improvement
1 parent 2e45de7 commit 4c6c053

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

app/code/Magento/Backend/etc/adminhtml/system.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,15 @@
480480
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
481481
</field>
482482
</group>
483+
<group id="grid" translate="label" type="text" sortOrder="45" showInDefault="1">
484+
<label>Admin Grids</label>
485+
<field id="show_approximate_total_number_of_products" translate="label comment"
486+
type="select" sortOrder="1" showInDefault="1" canRestore="1">
487+
<label>Show approximate total number of products</label>
488+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
489+
<comment>Show approximate total number of products in products grid header.</comment>
490+
</field>
491+
</group>
483492
</section>
484493
<section id="web" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
485494
<label>Web</label>

app/code/Magento/Backend/etc/config.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
<max_height>1200</max_height>
3232
</upload_configuration>
3333
</system>
34+
<admin>
35+
<grid>
36+
<show_approximate_total_number_of_products>1</show_approximate_total_number_of_products>
37+
</grid>
38+
</admin>
3439
<general>
3540
<validator_data>
3641
<input_types>

app/code/Magento/Catalog/Ui/DataProvider/Product/ProductCollection.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Catalog\Ui\DataProvider\Product;
77

8+
use Magento\Framework\DB\Select;
9+
810
/**
911
* Collection which is used for rendering product list in the backend.
1012
*
@@ -25,4 +27,56 @@ protected function _productLimitationJoinPrice()
2527
$this->_productLimitationFilters->setUsePriceIndex(false);
2628
return $this->_productLimitationPrice(true);
2729
}
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function getSize()
35+
{
36+
if ($this->_scopeConfig->getValue('admin/grid/show_approximate_total_number_of_products')) {
37+
$sql = $this->getSelectCountSql();
38+
return $this->analyzeCount($sql);
39+
}
40+
41+
return parent::getSize();
42+
}
43+
44+
/**
45+
* Analyze amount of entities in DB.
46+
*
47+
* @param $sql
48+
* @return int|mixed
49+
* @throws \Zend_Db_Select_Exception
50+
* @throws \Zend_Db_Statement_Exception
51+
*/
52+
private function analyzeCount($sql)
53+
{
54+
$results = $this->getConnection()->query('EXPLAIN ' . $sql)->fetchAll();
55+
$alias = $this->getMainTableAlias();
56+
57+
foreach ($results as $result) {
58+
if ($result['table'] == $alias) {
59+
return $result['rows'];
60+
}
61+
}
62+
63+
return 0;
64+
}
65+
66+
/**
67+
* Identify main table alias or its name if alias is not defined.
68+
*
69+
* @return string
70+
* @throws \LogicException
71+
* @throws \Zend_Db_Select_Exception
72+
*/
73+
private function getMainTableAlias()
74+
{
75+
foreach ($this->getSelect()->getPart(Select::FROM) as $tableAlias => $tableMetadata) {
76+
if ($tableMetadata['joinType'] == 'from') {
77+
return $tableAlias;
78+
}
79+
}
80+
throw new \LogicException("Main table cannot be identified.");
81+
}
2882
}

app/code/Magento/Sales/etc/adminhtml/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,9 @@
5252
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
5353
<plugin name="orderGridCollectionFilterPlugin" type="Magento\Sales\Plugin\Model\ResourceModel\Order\OrderGridCollectionFilter"/>
5454
</type>
55+
<type name="Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\DataProvider\ProductCollection">
56+
<arguments>
57+
<argument name="collectionFactory" xsi:type="object">\Magento\Catalog\Ui\DataProvider\Product\ProductCollectionFactory</argument>
58+
</arguments>
59+
</type>
5560
</config>

0 commit comments

Comments
 (0)