66
77namespace Magento \CatalogSearch \Controller \Result ;
88
9- use Magento \Catalog \Model \Product \ProductList \Toolbar ;
109use Magento \Framework \App \Action \HttpGetActionInterface ;
1110use Magento \Catalog \Model \Layer \Resolver ;
1211use Magento \Catalog \Model \Session ;
1514use Magento \Store \Model \StoreManagerInterface ;
1615use Magento \Search \Model \QueryFactory ;
1716use Magento \Search \Model \PopularSearchTerms ;
17+ use Magento \Catalog \Model \Product \ProductList \ToolbarMemorizer ;
18+ use Magento \Catalog \Model \Product \ProductList \Toolbar ;
1819
1920/**
2021 * Search result.
@@ -27,6 +28,8 @@ class Index extends \Magento\Framework\App\Action\Action implements HttpGetActio
2728 public const DEFAULT_NO_RESULT_HANDLE = 'catalogsearch_result_index_noresults ' ;
2829
2930 /**
31+ * Catalog session for storing catalog-related data
32+ *
3033 * @var Session
3134 */
3235 protected $ _catalogSession ;
@@ -42,29 +45,40 @@ class Index extends \Magento\Framework\App\Action\Action implements HttpGetActio
4245 private $ _queryFactory ;
4346
4447 /**
48+ * Catalog Layer Resolver
49+ *
4550 * @var Resolver
4651 */
4752 private $ layerResolver ;
4853
54+ /**
55+ * @var ToolbarMemorizer
56+ */
57+ private $ toolbarMemorizer ;
58+
4959 /**
5060 * @param Context $context
5161 * @param Session $catalogSession
5262 * @param StoreManagerInterface $storeManager
5363 * @param QueryFactory $queryFactory
5464 * @param Resolver $layerResolver
65+ * @param ToolbarMemorizer|null $toolbarMemorizer
5566 */
5667 public function __construct (
5768 Context $ context ,
5869 Session $ catalogSession ,
5970 StoreManagerInterface $ storeManager ,
6071 QueryFactory $ queryFactory ,
61- Resolver $ layerResolver
72+ Resolver $ layerResolver ,
73+ ?ToolbarMemorizer $ toolbarMemorizer = null
6274 ) {
6375 parent ::__construct ($ context );
6476 $ this ->_storeManager = $ storeManager ;
6577 $ this ->_catalogSession = $ catalogSession ;
6678 $ this ->_queryFactory = $ queryFactory ;
6779 $ this ->layerResolver = $ layerResolver ;
80+ $ this ->toolbarMemorizer = $ toolbarMemorizer ?:
81+ \Magento \Framework \App \ObjectManager::getInstance ()->get (ToolbarMemorizer::class);
6882 }
6983
7084 /**
@@ -111,6 +125,11 @@ public function execute()
111125 $ handles [] = static ::DEFAULT_NO_RESULT_HANDLE ;
112126 }
113127
128+ if ($ this ->shouldRedirectOnToolbarAction ()) {
129+ $ this ->getResponse ()->setRedirect ($ this ->_redirect ->getRedirectUrl ());
130+ return ;
131+ }
132+
114133 if (empty ($ getAdditionalRequestParameters ) &&
115134 $ this ->_objectManager ->get (PopularSearchTerms::class)->isCacheable ($ queryText , $ storeId )
116135 ) {
@@ -173,4 +192,21 @@ private function getNotCacheableResult($catalogSearchHelper, $query, $handles)
173192 $ this ->getResponse ()->setNoCacheHeaders ();
174193 $ this ->_view ->renderLayout ();
175194 }
195+
196+ /**
197+ * Checks for toolbar actions
198+ *
199+ * @return bool
200+ */
201+ private function shouldRedirectOnToolbarAction (): bool
202+ {
203+ $ params = $ this ->getRequest ()->getParams ();
204+
205+ return $ this ->toolbarMemorizer ->isMemorizingAllowed () && empty (array_intersect ([
206+ Toolbar::ORDER_PARAM_NAME ,
207+ Toolbar::DIRECTION_PARAM_NAME ,
208+ Toolbar::MODE_PARAM_NAME ,
209+ Toolbar::LIMIT_PARAM_NAME
210+ ], array_keys ($ params ))) === false ;
211+ }
176212}
0 commit comments