-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolving an issue where visibility filter for PDP requests was skipp…
…ed due to ES query emulation
- Loading branch information
1 parent
6974ea2
commit 747a863
Showing
4 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
.../Resolver/Products/DataProvider/Product/CollectionProcessor/VisibilityStatusProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
/** | ||
* @category ScandiPWA | ||
* @package ScandiPWA_CatalogGraphQl | ||
* @author Aleksandrs Mokans <[email protected]> | ||
* @copyright Copyright (c) 2022 Scandiweb, Ltd (https://scandiweb.com) | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Product\Collection; | ||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\VisibilityStatusProcessor as CoreVisibilityStatusProcessor; | ||
use Magento\Framework\Api\SearchCriteriaInterface; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\GraphQl\Model\Query\ContextInterface; | ||
use ScandiPWA\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CriteriaCheck; | ||
|
||
/** | ||
* Class VisibilityStatusProcessor | ||
* @package ScandiPWA\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor | ||
*/ | ||
class VisibilityStatusProcessor extends CoreVisibilityStatusProcessor | ||
{ | ||
/** | ||
* Process collection to add additional joins, attributes, and clauses to a product collection. | ||
* Rewrite: avoids joining the visibility attribute, if the filter was already present in searchCriteria | ||
* | ||
* @param Collection $collection | ||
* @param SearchCriteriaInterface $searchCriteria | ||
* @param array $attributeNames | ||
* @param ContextInterface|null $context | ||
* @return Collection | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
* @throws LocalizedException | ||
*/ | ||
public function process( | ||
Collection $collection, | ||
SearchCriteriaInterface $searchCriteria, | ||
array $attributeNames, | ||
ContextInterface $context = null | ||
): Collection { | ||
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner'); | ||
|
||
$visibilityFilter = CriteriaCheck::getVisibilityFilter($searchCriteria); | ||
|
||
if (!$visibilityFilter) { | ||
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner'); | ||
} | ||
|
||
return $collection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/** | ||
* @category ScandiPWA | ||
* @package ScandiPWA_CatalogGraphQl | ||
* @author Aleksandrs Mokans <[email protected]> | ||
* @copyright Copyright (c) 2022 Scandiweb, Ltd (https://scandiweb.com) | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace ScandiPWA\CatalogGraphQl\Plugin; | ||
|
||
use Magento\Framework\Api\FilterBuilder; | ||
use Magento\Framework\Api\Search\FilterGroupBuilder; | ||
use Magento\Framework\Api\SearchCriteriaInterface; | ||
use Magento\Framework\Api\Filter; | ||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch\ProductCollectionSearchCriteriaBuilder; | ||
use ScandiPWA\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CriteriaCheck; | ||
|
||
/** | ||
* Class AddVisibilityFilterOnSingleProductRequests | ||
* @package ScandiPWA\CatalogGraphQl\Model\Plugin | ||
*/ | ||
class AddVisibilityFilterOnSingleProductRequests | ||
{ | ||
/** | ||
* @var FilterBuilder | ||
*/ | ||
protected FilterBuilder $filterBuilder; | ||
|
||
/** | ||
* @var FilterGroupBuilder | ||
*/ | ||
protected FilterGroupBuilder $filterGroupBuilder; | ||
|
||
/** | ||
* @param FilterBuilder $filterBuilder | ||
* @param FilterGroupBuilder $filterGroupBuilder | ||
*/ | ||
public function __construct( | ||
FilterBuilder $filterBuilder, | ||
FilterGroupBuilder $filterGroupBuilder | ||
) { | ||
$this->filterBuilder = $filterBuilder; | ||
$this->filterGroupBuilder = $filterGroupBuilder; | ||
} | ||
|
||
/** | ||
* Applies visibility filter to single product collection loads | ||
* Necessary, because ES querywas skipped for performance | ||
* @param ProductCollectionSearchCriteriaBuilder $subject | ||
* @param SearchCriteriaInterface $searchCriteriaForCollection | ||
* @param SearchCriteriaInterface $searchCriteria | ||
* @return SearchCriteriaInterface | ||
*/ | ||
public function afterBuild( | ||
ProductCollectionSearchCriteriaBuilder $subject, | ||
SearchCriteriaInterface $searchCriteriaForCollection, | ||
SearchCriteriaInterface $searchCriteria | ||
): SearchCriteriaInterface { | ||
if (CriteriaCheck::isOnlySingleIdFilter($searchCriteria) && | ||
$visibilityFilter = CriteriaCheck::getVisibilityFilter($searchCriteria)) { | ||
$filterForCollection = $this->createVisibilityFilter($visibilityFilter); | ||
$this->filterGroupBuilder->addFilter($filterForCollection); | ||
$visibilityGroup = $this->filterGroupBuilder->create(); | ||
$filterGroups = $searchCriteriaForCollection->getFilterGroups(); | ||
$filterGroups[] = $visibilityGroup; | ||
$searchCriteriaForCollection->setFilterGroups($filterGroups); | ||
} | ||
|
||
return $searchCriteriaForCollection; | ||
} | ||
|
||
/** | ||
* Creates a collection filter based off of ES filter | ||
* @param Filter $filter | ||
* @return Filter | ||
*/ | ||
public function createVisibilityFilter(Filter $filter): Filter | ||
{ | ||
return $this->filterBuilder | ||
->setField($filter->getField()) | ||
->setValue($filter->getValue()) | ||
->setConditionType($filter->getConditionType()) | ||
->create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters