|
| 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 | +} |
0 commit comments