Skip to content

Commit 8d904e5

Browse files
committed
ACP2E-72: Search results suggestion give total count of 0 even though there are products
- Bug fixes.
1 parent 302f6c5 commit 8d904e5

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Search\Plugin\Model;
8+
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Search\Model\QueryResult;
11+
use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Get the search suggestion results count.
16+
*/
17+
class SearchSuggestionResultsCount
18+
{
19+
/**
20+
* Query collection factory
21+
*
22+
* @var QueryCollectionFactory
23+
*/
24+
protected QueryCollectionFactory $_queryCollectionFactory;
25+
26+
/**
27+
* Store manager
28+
*
29+
* @var StoreManagerInterface
30+
*/
31+
protected StoreManagerInterface $_storeManager;
32+
33+
/**
34+
* Construct Method for get the search suggestion results count.
35+
*
36+
* @param QueryCollectionFactory $queryCollectionFactory
37+
* @param StoreManagerInterface $storeManager
38+
*/
39+
public function __construct(
40+
QueryCollectionFactory $queryCollectionFactory,
41+
StoreManagerInterface $storeManager
42+
) {
43+
$this->_queryCollectionFactory = $queryCollectionFactory;
44+
$this->_storeManager = $storeManager;
45+
}
46+
47+
/**
48+
* Get the search suggestion results count.
49+
*
50+
* @throws NoSuchEntityException
51+
*/
52+
public function afterGetResultsCount(QueryResult $subject, $result)
53+
{
54+
$collection = $this->_queryCollectionFactory->create()->setStoreId(
55+
$this->_storeManager->getStore()->getId()
56+
)->setQueryFilter(
57+
$subject->getQueryText()
58+
);
59+
foreach ($collection as $item) {
60+
$result = $item->getNumResults();
61+
}
62+
return $result;
63+
}
64+
}

app/code/Magento/Search/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,7 @@
5656
</type>
5757
<preference for="Magento\Search\Setup\InstallConfigInterface" type="Magento\Search\Setup\CompositeInstallConfig"/>
5858
<preference for="Magento\Search\Model\SearchEngine\ValidatorInterface" type="Magento\Search\Model\SearchEngine\Validator"/>
59+
<type name="Magento\Search\Model\QueryResult">
60+
<plugin name="getSearchSuggestionResultsCount" type="Magento\Search\Plugin\Model\SearchSuggestionResultsCount" sortOrder="1"/>
61+
</type>
5962
</config>

0 commit comments

Comments
 (0)