Skip to content

Commit 23666ac

Browse files
committed
Add Catalog search action handle if no results
1 parent 6c529ec commit 23666ac

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

app/code/Magento/CatalogSearch/Controller/Advanced/Result.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
use Magento\Framework\UrlFactory;
1212

1313
/**
14+
* Catalog advanced search result
15+
*
1416
* @deprecated CatalogSearch will be removed in 2.4, and {@see \Magento\ElasticSearch}
1517
* will replace it as the default search engine.
1618
*/
1719
class Result extends \Magento\Framework\App\Action\Action
1820
{
21+
const DEFAULT_NO_RESULT_HANDLE = 'catalogsearch_advanced_result_noresults';
22+
1923
/**
2024
* Url factory
2125
*
@@ -48,13 +52,22 @@ public function __construct(
4852
}
4953

5054
/**
55+
* Run action
56+
*
5157
* @return \Magento\Framework\Controller\Result\Redirect
5258
*/
5359
public function execute()
5460
{
5561
try {
5662
$this->_catalogSearchAdvanced->addFilters($this->getRequest()->getQueryValue());
57-
$this->_view->loadLayout();
63+
$size = $this->_catalogSearchAdvanced->getProductCollection()->getSize();
64+
65+
$handles = null;
66+
if ($size == 0) {
67+
$handles = [static::DEFAULT_NO_RESULT_HANDLE];
68+
}
69+
70+
$this->_view->loadLayout($handles);
5871
$this->_view->renderLayout();
5972
} catch (\Magento\Framework\Exception\LocalizedException $e) {
6073
$this->messageManager->addError($e->getMessage());
@@ -66,4 +79,14 @@ public function execute()
6679
return $resultRedirect;
6780
}
6881
}
82+
83+
/**
84+
* Returns no result handle
85+
*
86+
* @return string
87+
*/
88+
private function getNoResultsHandle()
89+
{
90+
return self::DEFAULT_NO_RESULT_HANDLE;
91+
}
6992
}

app/code/Magento/CatalogSearch/Controller/Result/Index.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
use Magento\Search\Model\PopularSearchTerms;
1515

1616
/**
17+
* Catalog search result
18+
*
1719
* @deprecated CatalogSearch will be removed in 2.4, and {@see \Magento\ElasticSearch}
1820
* will replace it as the default search engine.
1921
*/
2022
class Index extends \Magento\Framework\App\Action\Action
2123
{
24+
const DEFAULT_NO_RESULT_HANDLE = 'catalogsearch_result_index_noresults';
25+
2226
/**
2327
* Catalog session
2428
*
@@ -89,12 +93,17 @@ public function execute()
8993
$getAdditionalRequestParameters = $this->getRequest()->getParams();
9094
unset($getAdditionalRequestParameters[QueryFactory::QUERY_VAR_NAME]);
9195

96+
$handles = null;
97+
if ($query->getNumResults() == 0) {
98+
$handles = [static::DEFAULT_NO_RESULT_HANDLE];
99+
}
100+
92101
if (empty($getAdditionalRequestParameters) &&
93102
$this->_objectManager->get(PopularSearchTerms::class)->isCacheable($queryText, $storeId)
94103
) {
95-
$this->getCacheableResult($catalogSearchHelper, $query);
104+
$this->getCacheableResult($catalogSearchHelper, $query, $handles);
96105
} else {
97-
$this->getNotCacheableResult($catalogSearchHelper, $query);
106+
$this->getNotCacheableResult($catalogSearchHelper, $query, $handles);
98107
}
99108
} else {
100109
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
@@ -106,9 +115,10 @@ public function execute()
106115
*
107116
* @param \Magento\CatalogSearch\Helper\Data $catalogSearchHelper
108117
* @param \Magento\Search\Model\Query $query
118+
* @param array $handles
109119
* @return void
110120
*/
111-
private function getCacheableResult($catalogSearchHelper, $query)
121+
private function getCacheableResult($catalogSearchHelper, $query, $handles)
112122
{
113123
if (!$catalogSearchHelper->isMinQueryLength()) {
114124
$redirect = $query->getRedirect();
@@ -120,7 +130,7 @@ private function getCacheableResult($catalogSearchHelper, $query)
120130

121131
$catalogSearchHelper->checkNotes();
122132

123-
$this->_view->loadLayout();
133+
$this->_view->loadLayout($handles);
124134
$this->_view->renderLayout();
125135
}
126136

@@ -129,11 +139,12 @@ private function getCacheableResult($catalogSearchHelper, $query)
129139
*
130140
* @param \Magento\CatalogSearch\Helper\Data $catalogSearchHelper
131141
* @param \Magento\Search\Model\Query $query
142+
* @param array $handles
132143
* @return void
133144
*
134145
* @throws \Magento\Framework\Exception\LocalizedException
135146
*/
136-
private function getNotCacheableResult($catalogSearchHelper, $query)
147+
private function getNotCacheableResult($catalogSearchHelper, $query, $handles)
137148
{
138149
if ($catalogSearchHelper->isMinQueryLength()) {
139150
$query->setId(0)->setIsActive(1)->setIsProcessed(1);
@@ -148,7 +159,7 @@ private function getNotCacheableResult($catalogSearchHelper, $query)
148159

149160
$catalogSearchHelper->checkNotes();
150161

151-
$this->_view->loadLayout();
162+
$this->_view->loadLayout($handles);
152163
$this->getResponse()->setNoCacheHeaders();
153164
$this->_view->renderLayout();
154165
}

app/code/Magento/CatalogSearch/Test/Unit/Controller/Advanced/ResultTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ function () use (&$filters, $expectedQuery) {
2727
$request = $this->createPartialMock(\Magento\Framework\App\Console\Request::class, ['getQueryValue']);
2828
$request->expects($this->once())->method('getQueryValue')->will($this->returnValue($expectedQuery));
2929

30+
$collection = $this->createPartialMock(
31+
\Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection::class,
32+
['getSize']
33+
);
34+
$collection->expects($this->once())->method('getSize')->will($this->returnValue(1));
35+
3036
$catalogSearchAdvanced = $this->createPartialMock(
3137
\Magento\CatalogSearch\Model\Advanced::class,
32-
['addFilters', '__wakeup']
38+
['addFilters', '__wakeup', 'getProductCollection']
3339
);
3440
$catalogSearchAdvanced->expects($this->once())->method('addFilters')->will(
3541
$this->returnCallback(
@@ -38,6 +44,8 @@ function ($added) use (&$filters) {
3844
}
3945
)
4046
);
47+
$catalogSearchAdvanced->expects($this->once())->method('getProductCollection')
48+
->will($this->returnValue($collection));
4149

4250
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
4351
$context = $objectManager->getObject(
@@ -138,4 +146,43 @@ public function testUrlSetOnException()
138146
);
139147
$this->assertEquals($redirectResultMock, $instance->execute());
140148
}
149+
150+
public function testNoResultsHandle()
151+
{
152+
$expectedQuery = 'notExistTerm';
153+
154+
$view = $this->createPartialMock(\Magento\Framework\App\View::class, ['loadLayout', 'renderLayout']);
155+
$view->expects($this->once())->method('loadLayout')
156+
->with([\Magento\CatalogSearch\Controller\Advanced\Result::DEFAULT_NO_RESULT_HANDLE]);
157+
158+
$request = $this->createPartialMock(\Magento\Framework\App\Console\Request::class, ['getQueryValue']);
159+
$request->expects($this->once())->method('getQueryValue')->will($this->returnValue($expectedQuery));
160+
161+
$collection = $this->createPartialMock(
162+
\Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection::class,
163+
['getSize']
164+
);
165+
$collection->expects($this->once())->method('getSize')->will($this->returnValue(0));
166+
167+
$catalogSearchAdvanced = $this->createPartialMock(
168+
\Magento\CatalogSearch\Model\Advanced::class,
169+
['addFilters', '__wakeup', 'getProductCollection']
170+
);
171+
172+
$catalogSearchAdvanced->expects($this->once())->method('getProductCollection')
173+
->will($this->returnValue($collection));
174+
175+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
176+
$context = $objectManager->getObject(
177+
\Magento\Framework\App\Action\Context::class,
178+
['view' => $view, 'request' => $request]
179+
);
180+
181+
/** @var \Magento\CatalogSearch\Controller\Advanced\Result $instance */
182+
$instance = $objectManager->getObject(
183+
\Magento\CatalogSearch\Controller\Advanced\Result::class,
184+
['context' => $context, 'catalogSearchAdvanced' => $catalogSearchAdvanced]
185+
);
186+
$instance->execute();
187+
}
141188
}

0 commit comments

Comments
 (0)