|
9 | 9 | use Magento\Catalog\Model\Layer\Resolver;
|
10 | 10 | use Magento\Catalog\Model\Session;
|
11 | 11 | use Magento\Framework\App\Action\Context;
|
12 |
| -use Magento\Framework\App\ResourceConnection; |
13 | 12 | use Magento\Store\Model\StoreManagerInterface;
|
14 | 13 | use Magento\Search\Model\QueryFactory;
|
| 14 | +use Magento\Search\Model\PopularSearchTerms; |
15 | 15 |
|
16 | 16 | class Index extends \Magento\Framework\App\Action\Action
|
17 | 17 | {
|
@@ -64,34 +64,89 @@ public function __construct(
|
64 | 64 | * Display search result
|
65 | 65 | *
|
66 | 66 | * @return void
|
| 67 | + * |
| 68 | + * @throws \Magento\Framework\Exception\LocalizedException |
67 | 69 | */
|
68 | 70 | public function execute()
|
69 | 71 | {
|
70 | 72 | $this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH);
|
| 73 | + |
71 | 74 | /* @var $query \Magento\Search\Model\Query */
|
72 | 75 | $query = $this->_queryFactory->get();
|
73 | 76 |
|
74 |
| - $query->setStoreId($this->_storeManager->getStore()->getId()); |
| 77 | + $storeId = $this->_storeManager->getStore()->getId(); |
| 78 | + $query->setStoreId($storeId); |
| 79 | + |
| 80 | + $queryText = $query->getQueryText(); |
| 81 | + |
| 82 | + if ($queryText != '') { |
| 83 | + $catalogSearchHelper = $this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class); |
75 | 84 |
|
76 |
| - if ($query->getQueryText() != '') { |
77 |
| - if ($this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class)->isMinQueryLength()) { |
78 |
| - $query->setId(0)->setIsActive(1)->setIsProcessed(1); |
| 85 | + $getAdditionalRequestParameters = $this->getRequest()->getParams(); |
| 86 | + unset($getAdditionalRequestParameters[$this->_queryFactory::QUERY_VAR_NAME]); |
| 87 | + |
| 88 | + if ( |
| 89 | + empty($getAdditionalRequestParameters) && |
| 90 | + $this->_objectManager->get(PopularSearchTerms::class)->isCacheable($queryText, $storeId) |
| 91 | + ) { |
| 92 | + $this->getCacheableResult($catalogSearchHelper, $query); |
79 | 93 | } else {
|
80 |
| - $query->saveIncrementalPopularity(); |
| 94 | + $this->getNotCacheableResult($catalogSearchHelper, $query); |
| 95 | + } |
| 96 | + } else { |
| 97 | + $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl()); |
| 98 | + } |
| 99 | + } |
81 | 100 |
|
82 |
| - $redirect = $query->getRedirect(); |
83 |
| - if ($redirect && $this->_url->getCurrentUrl() !== $redirect) { |
84 |
| - $this->getResponse()->setRedirect($redirect); |
85 |
| - return; |
86 |
| - } |
| 101 | + /** |
| 102 | + * Return cacheable result |
| 103 | + * |
| 104 | + * @param \Magento\CatalogSearch\Helper\Data $catalogSearchHelper |
| 105 | + * @param \Magento\Search\Model\Query $query |
| 106 | + * @return void |
| 107 | + */ |
| 108 | + private function getCacheableResult($catalogSearchHelper, $query) |
| 109 | + { |
| 110 | + if (!$catalogSearchHelper->isMinQueryLength()) { |
| 111 | + $redirect = $query->getRedirect(); |
| 112 | + if ($redirect && $this->_url->getCurrentUrl() !== $redirect) { |
| 113 | + $this->getResponse()->setRedirect($redirect); |
| 114 | + return; |
87 | 115 | }
|
| 116 | + } |
88 | 117 |
|
89 |
| - $this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class)->checkNotes(); |
| 118 | + $catalogSearchHelper->checkNotes(); |
| 119 | + |
| 120 | + $this->_view->loadLayout(); |
| 121 | + $this->_view->renderLayout(); |
| 122 | + } |
90 | 123 |
|
91 |
| - $this->_view->loadLayout(); |
92 |
| - $this->_view->renderLayout(); |
| 124 | + /** |
| 125 | + * Return not cacheable result |
| 126 | + * |
| 127 | + * @param \Magento\CatalogSearch\Helper\Data $catalogSearchHelper |
| 128 | + * @param \Magento\Search\Model\Query $query |
| 129 | + * @return void |
| 130 | + * |
| 131 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 132 | + */ |
| 133 | + private function getNotCacheableResult($catalogSearchHelper, $query) |
| 134 | + { |
| 135 | + if ($catalogSearchHelper->isMinQueryLength()) { |
| 136 | + $query->setId(0)->setIsActive(1)->setIsProcessed(1); |
93 | 137 | } else {
|
94 |
| - $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl()); |
| 138 | + $query->saveIncrementalPopularity(); |
| 139 | + $redirect = $query->getRedirect(); |
| 140 | + if ($redirect && $this->_url->getCurrentUrl() !== $redirect) { |
| 141 | + $this->getResponse()->setRedirect($redirect); |
| 142 | + return; |
| 143 | + } |
95 | 144 | }
|
| 145 | + |
| 146 | + $catalogSearchHelper->checkNotes(); |
| 147 | + |
| 148 | + $this->_view->loadLayout(); |
| 149 | + $this->getResponse()->setNoCacheHeaders(); |
| 150 | + $this->_view->renderLayout(); |
96 | 151 | }
|
97 | 152 | }
|
0 commit comments