From dacf941f8c0401dfce2cd8bfef645f8a11a281d9 Mon Sep 17 00:00:00 2001 From: Alfreds Genkins Date: Thu, 27 Aug 2020 13:15:26 +0300 Subject: [PATCH] Fixed price groups, fixed https://github.com/scandipwa/base-theme/issues/887 --- .../Context/AddSearchCriteriaToContext.php | 2 - src/Model/Resolver/Product/PriceRange.php | 2 +- .../CollectionProcessor/PriceProcessor.php | 7 -- src/Model/Resolver/Products/Query/Search.php | 3 +- src/Model/Variant/Collection.php | 24 +------ src/Plugin/Resolver/Products.php | 68 +++++++++++++++++++ src/etc/graphql/di.xml | 9 +++ 7 files changed, 81 insertions(+), 34 deletions(-) create mode 100644 src/Plugin/Resolver/Products.php diff --git a/src/Model/Context/AddSearchCriteriaToContext.php b/src/Model/Context/AddSearchCriteriaToContext.php index ebb07d2..71cf29e 100644 --- a/src/Model/Context/AddSearchCriteriaToContext.php +++ b/src/Model/Context/AddSearchCriteriaToContext.php @@ -25,9 +25,7 @@ class AddSearchCriteriaToContext implements ContextParametersProcessorInterface public function execute( ContextParametersInterface $contextParameters ) : ContextParametersInterface { - $contextParameters->addExtensionAttribute('search_criteria', null); - return $contextParameters; } } diff --git a/src/Model/Resolver/Product/PriceRange.php b/src/Model/Resolver/Product/PriceRange.php index e52f09c..4c56b28 100644 --- a/src/Model/Resolver/Product/PriceRange.php +++ b/src/Model/Resolver/Product/PriceRange.php @@ -9,6 +9,7 @@ use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount; use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool; +use Magento\Framework\Api\SearchCriteriaInterface; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Catalog\Model\Product; @@ -68,7 +69,6 @@ public function resolve( /** @var Product $product */ $product = $value['model']; - // literally single line got removed here $requestedFields = $info->getFieldSelection(10); $returnArray = []; diff --git a/src/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/PriceProcessor.php b/src/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/PriceProcessor.php index f821f27..d628077 100644 --- a/src/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/PriceProcessor.php +++ b/src/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/PriceProcessor.php @@ -19,7 +19,6 @@ class PriceProcessor implements CollectionProcessorInterface { const PRICE_FIELD = 'price_range'; - const FLAG_CUSTOMER_GROUP_PRICE_ADDED = 'customer_group_price_data_added'; /** * {@inheritdoc} @@ -29,15 +28,9 @@ public function process( SearchCriteriaInterface $searchCriteria, array $attributeNames ): Collection { - $isPriceDataAdded = $collection->getFlag(self::FLAG_CUSTOMER_GROUP_PRICE_ADDED); - // add tax percent, no-matter what $collection->addTaxPercents(); - if ($isPriceDataAdded) { - return $collection; - } - if (in_array(self::PRICE_FIELD, $attributeNames, true)) { /** @var $collection Collection */ $collection->addPriceData(); diff --git a/src/Model/Resolver/Products/Query/Search.php b/src/Model/Resolver/Products/Query/Search.php index d1aabd8..d0b06d0 100644 --- a/src/Model/Resolver/Products/Query/Search.php +++ b/src/Model/Resolver/Products/Query/Search.php @@ -7,6 +7,7 @@ namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\Query; +use Exception; use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder; use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; @@ -112,7 +113,7 @@ public function __construct( * @param array $args * @param ResolveInfo $info * @return SearchResult - * @throws \Exception + * @throws Exception */ public function getResult( array $args, diff --git a/src/Model/Variant/Collection.php b/src/Model/Variant/Collection.php index df18621..3163a16 100755 --- a/src/Model/Variant/Collection.php +++ b/src/Model/Variant/Collection.php @@ -254,31 +254,9 @@ protected function getChildCollectionMapAndList(): array { protected function getSearchCriteria(array $childrenIds): SearchCriteriaInterface { // build a search criteria based on original one and filter of product ids - $searchCriteria = $this->searchCriteriaBuilder + return $this->searchCriteriaBuilder ->addFilter('entity_id', $childrenIds, 'in') ->create(); - - $isSingleProduct = CriteriaCheck::isSingleProductFilter($this->searchCriteria); - - $customFilterGroups = $searchCriteria->getFilterGroups(); - $originalFilterGroups = $this->searchCriteria->getFilterGroups(); - - if (!$isSingleProduct) { - $filterGroups = array_merge($customFilterGroups, $originalFilterGroups); - } else { - // special case for customer group price - it is needed to be added to filter - foreach ($originalFilterGroups as $filterGroup) { - foreach ($filterGroup->getFilters() as $filter) { - if ($filter->getField() === 'customer_group_id') { - $filterGroups = array_merge($customFilterGroups, [$filterGroup]); - } - } - } - } - - $searchCriteria->setFilterGroups($filterGroups ?? $customFilterGroups); - - return $searchCriteria; } /** diff --git a/src/Plugin/Resolver/Products.php b/src/Plugin/Resolver/Products.php new file mode 100644 index 0000000..d36a910 --- /dev/null +++ b/src/Plugin/Resolver/Products.php @@ -0,0 +1,68 @@ + + * @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) + */ + +declare(strict_types=1); + +namespace ScandiPWA\CatalogGraphQl\Plugin\Resolver; + +use Magento\CatalogGraphQl\Model\Resolver\Products as CoreProducts; +use Magento\Customer\Model\Session; +use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder; +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; + +class Products { + /** @var Builder */ + protected $searchCriteriaBuilder; + + /** @var Session */ + protected $customerSession; + + /** + * Products constructor. + * @param Builder $searchCriteriaBuilder + * @param Session $customerSession + */ + public function __construct( + Builder $searchCriteriaBuilder, + Session $customerSession + ) { + $this->searchCriteriaBuilder = $searchCriteriaBuilder; + $this->customerSession = $customerSession; + } + + public function beforeResolve( + CoreProducts $products, + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + $searchCriteria = $this->searchCriteriaBuilder->build('products', $args); + $context->getExtensionAttributes()->setSearchCriteria($searchCriteria); + + foreach ($searchCriteria->getFilterGroups() as $filterGroup) { + foreach ($filterGroup->getFilters() as $filter) { + if ($filter->getField() === 'customer_group_id') { + $this->customerSession->setCustomerGroupId($filter->getValue()); + } + } + } + + return [ + $field, + $context, + $info, + $value, + $args + ]; + } +} diff --git a/src/etc/graphql/di.xml b/src/etc/graphql/di.xml index a72aacd..6980f0f 100644 --- a/src/etc/graphql/di.xml +++ b/src/etc/graphql/di.xml @@ -20,6 +20,15 @@ /> + + + +