From ea34130b09557558f6d08430020be15e0165acaa Mon Sep 17 00:00:00 2001 From: Herant Date: Wed, 13 Oct 2021 16:03:20 +0200 Subject: [PATCH 1/3] Implement logic for retrieving first page if pagination yields no result after filtering --- .../Catalog/Layer/ItemCollectionProvider.php | 11 +++++++- Model/Catalog/Layer/NavigationContext.php | 28 +++++++++++++++++-- .../Url/Strategy/QueryParameterStrategy.php | 3 +- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Model/Catalog/Layer/ItemCollectionProvider.php b/Model/Catalog/Layer/ItemCollectionProvider.php index 47c7c0fb..8d9f5a5b 100644 --- a/Model/Catalog/Layer/ItemCollectionProvider.php +++ b/Model/Catalog/Layer/ItemCollectionProvider.php @@ -75,7 +75,16 @@ public function getCollection(Category $category) } try { - return $this->collectionFactory->create(['navigationContext' => $this->navigationContext]); + /** @var Collection $collection */ + $collection = $this->collectionFactory->create(['navigationContext' => $this->navigationContext]); + + if (count($collection->getItems()) < 1) { + $collection = $this->collectionFactory + ->create(['navigationContext' => $this->navigationContext->resetPagination()]) + ; + } + + return $collection; } catch (TweakwiseException $e) { $this->log->critical($e); $this->config->setTweakwiseExceptionThrown(); diff --git a/Model/Catalog/Layer/NavigationContext.php b/Model/Catalog/Layer/NavigationContext.php index 6abd5176..0ac97161 100644 --- a/Model/Catalog/Layer/NavigationContext.php +++ b/Model/Catalog/Layer/NavigationContext.php @@ -36,7 +36,7 @@ class NavigationContext /** * @var ProductNavigationRequest */ - protected $request; + protected $request = null; /** * @var RequestFactory @@ -49,9 +49,9 @@ class NavigationContext protected $client; /** - * @var ProductNavigationResponse + * @var ProductNavigationResponse|null */ - protected $response; + protected $response = null; /** * @var Url @@ -150,6 +150,7 @@ public function getResponse(): ProductNavigationResponse { if (!$this->response) { $request = $this->getRequest(); + $this->initializeRequest($request); $this->response = $this->client->request($request); @@ -168,6 +169,27 @@ public function hasResponse(): bool return $this->response !== null; } + /** + * @return $this + */ + public function resetPagination(): self + { + $params = $this->request->getParameters(); + unset($params['tn_p']); + unset($params['tn_fk_p']); + $params['resetPagination'] = true; + $params['tn_fk_p'] = 1; + $params['tn_p'] = 1; + + $this->request = null; + + $request = $this->getRequest()->setParameters($params); + + $this->response = null; + + return $this->initializeRequest($request); + } + /** * @param $attributeCodes * @return Attribute[] diff --git a/Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php b/Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php index 5daa4f4d..4e433acc 100644 --- a/Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php +++ b/Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php @@ -345,7 +345,8 @@ public function apply(MagentoHttpRequest $request, ProductNavigationRequest $nav } $page = $this->getPage($request); - if ($page) { + + if ($page && (bool) $navigationRequest->getParameter('resetPagination') === false) { $navigationRequest->setPage($page); } From ca836fa0e5be07ce47ba105d193268820215f026 Mon Sep 17 00:00:00 2001 From: Herant Date: Wed, 13 Oct 2021 16:04:16 +0200 Subject: [PATCH 2/3] Implement logic for retrieving first page if pagination yields no result after filtering --- Model/Catalog/Layer/ItemCollectionProvider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Model/Catalog/Layer/ItemCollectionProvider.php b/Model/Catalog/Layer/ItemCollectionProvider.php index 8d9f5a5b..f2d3915e 100644 --- a/Model/Catalog/Layer/ItemCollectionProvider.php +++ b/Model/Catalog/Layer/ItemCollectionProvider.php @@ -75,7 +75,6 @@ public function getCollection(Category $category) } try { - /** @var Collection $collection */ $collection = $this->collectionFactory->create(['navigationContext' => $this->navigationContext]); if (count($collection->getItems()) < 1) { From ec0fd7ad804d71c8ff73c971819bec26539e29ed Mon Sep 17 00:00:00 2001 From: Herant Date: Fri, 15 Oct 2021 11:27:40 +0200 Subject: [PATCH 3/3] Update tweakwise pagination param values when reset pagination is requested --- Model/Catalog/Layer/NavigationContext.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Model/Catalog/Layer/NavigationContext.php b/Model/Catalog/Layer/NavigationContext.php index 0ac97161..61dacc6f 100644 --- a/Model/Catalog/Layer/NavigationContext.php +++ b/Model/Catalog/Layer/NavigationContext.php @@ -175,8 +175,6 @@ public function hasResponse(): bool public function resetPagination(): self { $params = $this->request->getParameters(); - unset($params['tn_p']); - unset($params['tn_fk_p']); $params['resetPagination'] = true; $params['tn_fk_p'] = 1; $params['tn_p'] = 1;