Skip to content

Commit

Permalink
Use plugin instead of preference
Browse files Browse the repository at this point in the history
  • Loading branch information
jansentjeu committed Oct 25, 2024
1 parent bbfc9b5 commit c399c9d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 88 deletions.
87 changes: 0 additions & 87 deletions src/Block/Product/ListProduct.php

This file was deleted.

78 changes: 78 additions & 0 deletions src/Plugin/Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Tweakwise\AttributeLandingTweakwise\Plugin\Block\Product;

use Emico\AttributeLanding\Api\LandingPageRepositoryInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Tweakwise\Magento2Tweakwise\Block\Product\ListProduct as Subject;
use Tweakwise\Magento2Tweakwise\Helper\Cache;

class ListProduct
{
/**
* @param RequestInterface $request
* @param Cache $cacheHelper
* @param LandingPageRepositoryInterface $landingPageRepository
* @param Registry $registry
*/
public function __construct(
private readonly RequestInterface $request,
private readonly Cache $cacheHelper,
private readonly LandingPageRepositoryInterface $landingPageRepository,
private readonly Registry $registry
) {
}

/**
* @param Subject $subject
* @param string $route
* @param array $params
* @return array
*/
public function beforeGetUrl(Subject $subject, $route = '', $params = [])
{
$landingPageId = (int)$this->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';
}
}
5 changes: 4 additions & 1 deletion src/etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Emico\AttributeLanding\Model\FilterHider\FilterHiderInterface" type="Tweakwise\AttributeLandingTweakwise\Model\FilterHider\TweakwiseFilterHider"/>
<preference for="Tweakwise\Magento2Tweakwise\Block\Product\ListProduct" type="Tweakwise\AttributeLandingTweakwise\Block\Product\ListProduct"/>
<type name="Emico\AttributeLanding\Model\FilterApplier\AggregateFilterApplier">
<arguments>
<argument name="appliers" xsi:type="array">
Expand Down Expand Up @@ -65,4 +64,8 @@
type="Tweakwise\AttributeLandingTweakwise\Plugin\Block\LayeredNavigation\RenderLayered\RendererPlugin"/>
</type>

<type name="Tweakwise\Magento2Tweakwise\Block\Product\ListProduct">
<plugin name="Tweakwise_AttributeLandingTweakwise_Plugin_Block_Product_ListProduct"
type="Tweakwise\AttributeLandingTweakwise\Plugin\Block\Product\ListProduct"/>
</type>
</config>

0 comments on commit c399c9d

Please sign in to comment.