Skip to content

Commit

Permalink
Merge pull request #3838 from magento-helix/MAGETWO-95294-3
Browse files Browse the repository at this point in the history
[Performance] Catalog search improvements
  • Loading branch information
duhon authored Mar 5, 2019
2 parents 3a4ba66 + e150e90 commit e2ca112
Show file tree
Hide file tree
Showing 73 changed files with 2,314 additions and 283 deletions.
23 changes: 17 additions & 6 deletions app/code/Magento/Catalog/Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ private function getDefaultListingMode()
}

/**
* Need use as _prepareLayout - but problem in declaring collection from
* another block (was problem with search result)
* Need use as _prepareLayout - but problem in declaring collection from another block.
* (was problem with search result)
*
* @return $this
*/
protected function _beforeToHtml()
Expand All @@ -188,7 +189,9 @@ protected function _beforeToHtml()

$this->addToolbarBlock($collection);

$collection->load();
if (!$collection->isLoaded()) {
$collection->load();
}

return parent::_beforeToHtml();
}
Expand Down Expand Up @@ -262,6 +265,8 @@ public function getToolbarHtml()
}

/**
* Set collection.
*
* @param AbstractCollection $collection
* @return $this
*/
Expand All @@ -272,7 +277,9 @@ public function setCollection($collection)
}

/**
* @param array|string|integer| Element $code
* Add attribute.
*
* @param array|string|integer|Element $code
* @return $this
*/
public function addAttribute($code)
Expand All @@ -282,6 +289,8 @@ public function addAttribute($code)
}

/**
* Get price block template.
*
* @return mixed
*/
public function getPriceBlockTemplate()
Expand Down Expand Up @@ -371,6 +380,8 @@ public function getAddToCartPostParams(Product $product)
}

/**
* Get product price.
*
* @param Product $product
* @return string
*/
Expand All @@ -396,8 +407,8 @@ public function getProductPrice(Product $product)
}

/**
* Specifies that price rendering should be done for the list of products
* i.e. rendering happens in the scope of product list, but not single product
* Specifies that price rendering should be done for the list of products.
* (rendering happens in the scope of product list, but not single product)
*
* @return Render
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace Magento\CatalogInventory\Model;

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Search\EngineResolverInterface;
use Magento\Search\Model\EngineResolver;

/**
* Catalog inventory module plugin
Expand All @@ -17,26 +19,37 @@ class AddStockStatusToCollection
* @var \Magento\CatalogInventory\Helper\Stock
*/
protected $stockHelper;


/**
* @var EngineResolverInterface
*/
private $engineResolver;

/**
* @param \Magento\CatalogInventory\Model\Configuration $configuration
* @param \Magento\CatalogInventory\Helper\Stock $stockHelper
* @param EngineResolverInterface $engineResolver
*/
public function __construct(
\Magento\CatalogInventory\Helper\Stock $stockHelper
\Magento\CatalogInventory\Helper\Stock $stockHelper,
EngineResolverInterface $engineResolver
) {
$this->stockHelper = $stockHelper;
$this->engineResolver = $engineResolver;
}

/**
* Add stock filter to collection.
*
* @param Collection $productCollection
* @param bool $printQuery
* @param bool $logQuery
* @return array
*/
public function beforeLoad(Collection $productCollection, $printQuery = false, $logQuery = false)
{
$this->stockHelper->addIsInStockFilterToCollection($productCollection);
if ($this->engineResolver->getCurrentSearchEngine() === EngineResolver::CATALOG_SEARCH_MYSQL_ENGINE) {
$this->stockHelper->addIsInStockFilterToCollection($productCollection);
}
return [$printQuery, $logQuery];
}
}
29 changes: 27 additions & 2 deletions app/code/Magento/CatalogInventory/Model/Plugin/Layer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogInventory\Model\Plugin;

use Magento\Framework\Search\EngineResolverInterface;
use Magento\Search\Model\EngineResolver;

/**
* Catalog inventory plugin for layer.
*/
class Layer
{
/**
Expand All @@ -21,16 +28,24 @@ class Layer
*/
protected $scopeConfig;

/**
* @var EngineResolverInterface
*/
private $engineResolver;

/**
* @param \Magento\CatalogInventory\Helper\Stock $stockHelper
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param EngineResolverInterface $engineResolver
*/
public function __construct(
\Magento\CatalogInventory\Helper\Stock $stockHelper,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
EngineResolverInterface $engineResolver
) {
$this->stockHelper = $stockHelper;
$this->scopeConfig = $scopeConfig;
$this->engineResolver = $engineResolver;
}

/**
Expand All @@ -46,12 +61,22 @@ public function beforePrepareProductCollection(
\Magento\Catalog\Model\Layer $subject,
\Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection
) {
if ($this->_isEnabledShowOutOfStock()) {
if (!$this->isCurrentEngineMysql() || $this->_isEnabledShowOutOfStock()) {
return;
}
$this->stockHelper->addIsInStockFilterToCollection($collection);
}

/**
* Check if current engine is MYSQL.
*
* @return bool
*/
private function isCurrentEngineMysql()
{
return $this->engineResolver->getCurrentSearchEngine() === EngineResolver::CATALOG_SEARCH_MYSQL_ENGINE;
}

/**
* Get config value for 'display out of stock' option
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\CatalogInventory\Test\Unit\Model;

use Magento\CatalogInventory\Model\AddStockStatusToCollection;
use Magento\Framework\Search\EngineResolverInterface;

class AddStockStatusToCollectionTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -19,13 +20,24 @@ class AddStockStatusToCollectionTest extends \PHPUnit\Framework\TestCase
*/
protected $stockHelper;

/**
* @var EngineResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $engineResolver;

protected function setUp()
{
$this->stockHelper = $this->createMock(\Magento\CatalogInventory\Helper\Stock::class);
$this->engineResolver = $this->getMockBuilder(EngineResolverInterface::class)
->disableOriginalConstructor()
->setMethods(['getCurrentSearchEngine'])
->getMockForAbstractClass();

$this->plugin = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
\Magento\CatalogInventory\Model\AddStockStatusToCollection::class,
[
'stockHelper' => $this->stockHelper,
'engineResolver' => $this->engineResolver
]
);
}
Expand All @@ -36,6 +48,10 @@ public function testAddStockStatusToCollection()
->disableOriginalConstructor()
->getMock();

$this->engineResolver->expects($this->any())
->method('getCurrentSearchEngine')
->willReturn('mysql');

$this->stockHelper->expects($this->once())
->method('addIsInStockFilterToCollection')
->with($productCollection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\CatalogInventory\Test\Unit\Model\Plugin;

use Magento\Framework\Search\EngineResolverInterface;

class LayerTest extends \PHPUnit\Framework\TestCase
{
/**
Expand All @@ -22,14 +24,24 @@ class LayerTest extends \PHPUnit\Framework\TestCase
*/
protected $_stockHelperMock;

/**
* @var EngineResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $engineResolver;

protected function setUp()
{
$this->_scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
$this->_stockHelperMock = $this->createMock(\Magento\CatalogInventory\Helper\Stock::class);
$this->engineResolver = $this->getMockBuilder(EngineResolverInterface::class)
->disableOriginalConstructor()
->setMethods(['getCurrentSearchEngine'])
->getMockForAbstractClass();

$this->_model = new \Magento\CatalogInventory\Model\Plugin\Layer(
$this->_stockHelperMock,
$this->_scopeConfigMock
$this->_scopeConfigMock,
$this->engineResolver
);
}

Expand All @@ -38,6 +50,10 @@ protected function setUp()
*/
public function testAddStockStatusDisabledShow()
{
$this->engineResolver->expects($this->any())
->method('getCurrentSearchEngine')
->willReturn('mysql');

$this->_scopeConfigMock->expects(
$this->once()
)->method(
Expand All @@ -60,6 +76,10 @@ public function testAddStockStatusDisabledShow()
*/
public function testAddStockStatusEnabledShow()
{
$this->engineResolver->expects($this->any())
->method('getCurrentSearchEngine')
->willReturn('mysql');

$this->_scopeConfigMock->expects(
$this->once()
)->method(
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/CatalogInventory/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"php": "~7.1.3||~7.2.0",
"magento/framework": "*",
"magento/module-catalog": "*",
"magento/module-search": "*",
"magento/module-config": "*",
"magento/module-customer": "*",
"magento/module-eav": "*",
Expand Down
12 changes: 3 additions & 9 deletions app/code/Magento/CatalogSearch/Controller/Advanced/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,9 @@ public function execute()
{
try {
$this->_catalogSearchAdvanced->addFilters($this->getRequest()->getQueryValue());
$size = $this->_catalogSearchAdvanced->getProductCollection()->getSize();

$handles = null;
if ($size == 0) {
$this->_view->getPage()->initLayout();
$handles = $this->_view->getLayout()->getUpdate()->getHandles();
$handles[] = static::DEFAULT_NO_RESULT_HANDLE;
}

$this->_view->getPage()->initLayout();
$handles = $this->_view->getLayout()->getUpdate()->getHandles();
$handles[] = static::DEFAULT_NO_RESULT_HANDLE;
$this->_view->loadLayout($handles);
$this->_view->renderLayout();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
Expand Down
Loading

0 comments on commit e2ca112

Please sign in to comment.