From c399c9d6bcaec8dd26d2cea50253b977f9439af8 Mon Sep 17 00:00:00 2001 From: tjeujansen Date: Fri, 25 Oct 2024 14:13:17 +0200 Subject: [PATCH] Use plugin instead of preference --- src/Block/Product/ListProduct.php | 87 ------------------------ src/Plugin/Block/Product/ListProduct.php | 78 +++++++++++++++++++++ src/etc/di.xml | 5 +- 3 files changed, 82 insertions(+), 88 deletions(-) delete mode 100644 src/Block/Product/ListProduct.php create mode 100644 src/Plugin/Block/Product/ListProduct.php diff --git a/src/Block/Product/ListProduct.php b/src/Block/Product/ListProduct.php deleted file mode 100644 index 41d018d..0000000 --- a/src/Block/Product/ListProduct.php +++ /dev/null @@ -1,87 +0,0 @@ -request->getModuleName() === 'emico_attributelanding') { - $landingPageId = (int)$this->request->getParam('id'); - if (!empty($landingPageId)) { - $landingPage = $this->landingPageRepository->getById($landingPageId); - $filters = $this->registry->registry('alp_filters' . $landingPageId); - $filterTemplate = $landingPage->getTweakwiseFilterTemplate(); - $sortTemplate = $landingPage->getTweakwiseSortTemplate(); - - foreach ($filters as $filter) { - $queryParams[$filter->getFacet()] = $filter->getValue(); - } - - if (!empty($filterTemplate)) { - $queryParams['tn_ft'] = $filterTemplate; - } - - if (!empty($sortTemplate)) { - $queryParams['tn_st'] = $sortTemplate; - } - } - } - - return parent::getUrl($route, $params, $queryParams); - } -} diff --git a/src/Plugin/Block/Product/ListProduct.php b/src/Plugin/Block/Product/ListProduct.php new file mode 100644 index 0000000..88b9fa5 --- /dev/null +++ b/src/Plugin/Block/Product/ListProduct.php @@ -0,0 +1,78 @@ +request->getParam('id'); + if ( + !$this->isAttributeLandingRequest() || + !$landingPageId || + !$this->cacheHelper->personalMerchandisingCanBeApplied() || + $route !== 'page_cache/block/esi' + ) { + return [$route, $params]; + } + + try { + $landingPage = $this->landingPageRepository->getById($landingPageId); + } catch (NoSuchEntityException | LocalizedException $e) { + return [$route, $params]; + } + + $filters = $this->registry->registry(sprintf('alp_filters_%s', $landingPageId)) ?? []; + $filterTemplate = $landingPage->getTweakwiseFilterTemplate(); + $sortTemplate = $landingPage->getTweakwiseSortTemplate(); + + foreach ($filters as $filter) { + $params['_query'][$filter->getFacet()] = $filter->getValue(); + } + + if (!empty($filterTemplate)) { + $params['_query']['tn_ft'] = $filterTemplate; + } + + if (!empty($sortTemplate)) { + $params['_query']['tn_st'] = $sortTemplate; + } + + return [$route, $params]; + } + + private function isAttributeLandingRequest(): bool + { + return $this->request->getModuleName() === 'emico_attributelanding'; + } +} diff --git a/src/etc/di.xml b/src/etc/di.xml index 17b038b..ac7fa49 100644 --- a/src/etc/di.xml +++ b/src/etc/di.xml @@ -1,7 +1,6 @@ - @@ -65,4 +64,8 @@ type="Tweakwise\AttributeLandingTweakwise\Plugin\Block\LayeredNavigation\RenderLayered\RendererPlugin"/> + + +