From cf59fed88b8a985298795756fcb39036396184c8 Mon Sep 17 00:00:00 2001 From: aleksandrskondratjevs Date: Tue, 4 Apr 2023 05:34:26 +0300 Subject: [PATCH] Fix compatibility issues with magento 2.4.6 --- Model/Resolver/CustomerOrders.php | 107 ++++++++++++++---- .../CustomerOrders/Query/OrderFilter.php | 10 +- 2 files changed, 96 insertions(+), 21 deletions(-) diff --git a/Model/Resolver/CustomerOrders.php b/Model/Resolver/CustomerOrders.php index 5583f28..66be58f 100755 --- a/Model/Resolver/CustomerOrders.php +++ b/Model/Resolver/CustomerOrders.php @@ -12,19 +12,20 @@ namespace ScandiPWA\SalesGraphQl\Model\Resolver; use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\InputException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; -use Magento\Sales\Api\Data\OrderSearchResultInterface; -use Magento\SalesGraphQl\Model\Resolver\CustomerOrders as CoreCustomerOrders; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\Sales\Api\Data\OrderSearchResultInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\SalesGraphQl\Model\Formatter\Order as OrderFormatter; +use Magento\SalesGraphQl\Model\Resolver\CustomerOrders as CoreCustomerOrders; use Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query\OrderFilter; +use Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query\OrderSort; use Magento\Store\Api\Data\StoreInterface; -use Magento\Framework\Api\SortOrderBuilder; -use Magento\Framework\Api\SortOrder; +use Magento\Store\Model\StoreManagerInterface; /** * Orders data resolver @@ -52,37 +53,46 @@ class CustomerOrders extends CoreCustomerOrders protected $orderFormatter; /** - * @var SortOrderBuilder + * @var OrderSort + */ + protected $orderSort; + + /** + * @var StoreManagerInterface|mixed|null */ - protected $sortOrderBuilder; + protected $storeManager; /** - * CustomerOrders constructor. * @param OrderRepositoryInterface $orderRepository * @param SearchCriteriaBuilder $searchCriteriaBuilder * @param OrderFilter $orderFilter * @param OrderFormatter $orderFormatter - * @param SortOrderBuilder $sortOrderBuilder + * @param OrderSort $orderSort + * @param StoreManagerInterface|null $storeManager */ public function __construct( OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $searchCriteriaBuilder, OrderFilter $orderFilter, OrderFormatter $orderFormatter, - SortOrderBuilder $sortOrderBuilder + SortOrderBuilder $sortOrderBuilder, + ?StoreManagerInterface $storeManager = null ) { parent::__construct( $orderRepository, $searchCriteriaBuilder, $orderFilter, - $orderFormatter + $orderFormatter, + $orderSort, + $storeManager ); $this->orderRepository = $orderRepository; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->orderFilter = $orderFilter; $this->orderFormatter = $orderFormatter; - $this->sortOrderBuilder = $sortOrderBuilder; + $this->orderSort = $orderSort; + $this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class); } /** @@ -90,7 +100,7 @@ public function __construct( */ public function resolve( Field $field, - $context, + $context, ResolveInfo $info, array $value = null, array $args = null @@ -107,12 +117,17 @@ public function resolve( throw new GraphQlInputException(__('pageSize value must be greater than 0.')); } + $storeIds = []; $userId = $context->getUserId(); /** @var StoreInterface $store */ $store = $context->getExtensionAttributes()->getStore(); + if (isset($args['scope'])) { + $storeIds = $this->getStoresByScope($args['scope'], $store); + } + try { - $searchResult = $this->getSearchResult($args, (int)$userId, (int)$store->getId()); + $searchResult = $this->getSearchResult($args, (int)$userId, (int)$store->getId(), $storeIds); $maxPages = (int)ceil($searchResult->getTotalCount() / $searchResult->getPageSize()); } catch (InputException $e) { throw new GraphQlInputException(__($e->getMessage())); @@ -126,7 +141,7 @@ public function resolve( return [ 'total_count' => $searchResult->getTotalCount(), - 'items' => $ordersArray, + 'items' => $ordersArray, 'page_info' => [ 'page_size' => $searchResult->getPageSize(), 'current_page' => $searchResult->getCurPage(), @@ -144,14 +159,11 @@ public function resolve( * @return OrderSearchResultInterface * @throws InputException */ - private function getSearchResult(array $args, int $userId, int $storeId) + private function getSearchResult(array $args, int $userId, int $storeId, array $storeIds) { - $filterGroups = $this->orderFilter->createFilterGroups($args, $userId, (int)$storeId); + $filterGroups = $this->orderFilter->createFilterGroups($args, $userId, (int)$storeId, $storeIds); $this->searchCriteriaBuilder->setFilterGroups($filterGroups); - $sortOrder = $this->sortOrderBuilder->setField('entity_id')->setDirection(SortOrder::SORT_DESC)->create(); - $this->searchCriteriaBuilder->setSortOrders([$sortOrder]); - if (isset($args['currentPage'])) { $this->searchCriteriaBuilder->setCurrentPage($args['currentPage']); } @@ -160,6 +172,63 @@ private function getSearchResult(array $args, int $userId, int $storeId) $this->searchCriteriaBuilder->setPageSize($args['pageSize']); } + if (isset($args['sort'])) { + $sortOrders = $this->orderSort->createSortOrders($args); + $this->searchCriteriaBuilder->setSortOrders($sortOrders); + } + return $this->orderRepository->getList($this->searchCriteriaBuilder->create()); } + + /** + * Get eligible store to filter by based on scope + * + * @param string $scope + * @param StoreInterface $store + * @return array + */ + private function getStoresByScope(string $scope, StoreInterface $store): array + { + $storeIds = []; + switch ($scope) { + case 'GLOBAL': + $storeIds = $this->getStoresByFilter(null, null); + break; + case 'WEBSITE': + $websiteId = $store->getWebsiteId(); + $storeIds = $this->getStoresByFilter((int)$websiteId, null); + break; + case 'STORE': + $storeGroupId = $store->getStoreGroupId(); + $storeIds = $this->getStoresByFilter(null, (int)$storeGroupId); + break; + default: + break; + } + return $storeIds; + } + + /** + * Filter store ids based on selected scope + * + * @param int|null $websiteId + * @param int|null $storeGroupId + * @return array + */ + private function getStoresByFilter(?int $websiteId, ?int $storeGroupId): array + { + $stores = $this->storeManager->getStores(true, true); + $storeIds = []; + foreach ($stores as $store) { + if (isset($websiteId) && $websiteId === (int)$store->getWebsiteId() + || + isset($storeGroupId) && $storeGroupId === (int)$store->getStoreGroupId() + ) { + $storeIds[] = $store->getId(); + } elseif (!isset($websiteId) && !isset($storeGroupId)) { + $storeIds[] = $store->getId(); + } + } + return $storeIds; + } } diff --git a/Model/Resolver/CustomerOrders/Query/OrderFilter.php b/Model/Resolver/CustomerOrders/Query/OrderFilter.php index 52d0bad..332b0d0 100644 --- a/Model/Resolver/CustomerOrders/Query/OrderFilter.php +++ b/Model/Resolver/CustomerOrders/Query/OrderFilter.php @@ -12,10 +12,10 @@ namespace ScandiPWA\SalesGraphQl\Model\Resolver\CustomerOrders\Query; use Magento\Framework\Api\FilterBuilder; +use Magento\Framework\Api\Search\FilterGroup; use Magento\Framework\Api\Search\FilterGroupBuilder; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\InputException; -use Magento\Framework\Api\Search\FilterGroup; use Magento\Sales\Model\Order\Config; use Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query\OrderFilter as CoreOrderFilter; @@ -91,7 +91,8 @@ public function __construct( public function createFilterGroups( array $args, int $userId, - int $storeId + int $storeId, + array $storeIds ): array { $filterGroups = []; @@ -104,6 +105,11 @@ public function createFilterGroups( $this->filterGroupBuilder->setFilters( [$this->filterBuilder->setField('status')->setValue($this->orderConfig->getVisibleOnFrontStatuses())->setConditionType('in')->create()] ); + + $storeIds[] = $storeId; + $this->filterGroupBuilder->setFilters( + [$this->filterBuilder->setField('store_id')->setValue($storeIds)->setConditionType('in')->create()] + ); $filterGroups[] = $this->filterGroupBuilder->create(); if (isset($args['filter'])) {