|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogCustomerGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Product\Collection; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 13 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 14 | +use Magento\Customer\Model\GroupManagement; |
| 15 | +use Magento\Framework\Exception\LocalizedException; |
| 16 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 17 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 18 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 19 | +use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; |
| 20 | +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
| 21 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 22 | + |
| 23 | +/** |
| 24 | + * @inheritdoc |
| 25 | + */ |
| 26 | +class TierPrices implements ResolverInterface |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var Collection |
| 30 | + */ |
| 31 | + private $collection; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var CustomerRepositoryInterface |
| 35 | + */ |
| 36 | + private $customerRepository; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ValueFactory |
| 40 | + */ |
| 41 | + private $valueFactory; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var int |
| 45 | + */ |
| 46 | + private $customerGroupId = null; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var array |
| 50 | + */ |
| 51 | + private $productIds = []; |
| 52 | + |
| 53 | + /** |
| 54 | + * @param CollectionFactory $collectionFactory |
| 55 | + * @param ValueFactory $valueFactory |
| 56 | + * @param CustomerRepositoryInterface $customerRepository |
| 57 | + */ |
| 58 | + public function __construct( |
| 59 | + CollectionFactory $collectionFactory, |
| 60 | + ValueFactory $valueFactory, |
| 61 | + CustomerRepositoryInterface $customerRepository |
| 62 | + ) { |
| 63 | + $this->collection = $collectionFactory->create(); |
| 64 | + $this->valueFactory = $valueFactory; |
| 65 | + $this->customerRepository = $customerRepository; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @inheritdoc |
| 70 | + */ |
| 71 | + public function resolve( |
| 72 | + Field $field, |
| 73 | + $context, |
| 74 | + ResolveInfo $info, |
| 75 | + array $value = null, |
| 76 | + array $args = null |
| 77 | + ) { |
| 78 | + if (!isset($value['model'])) { |
| 79 | + throw new LocalizedException(__('"model" value should be specified')); |
| 80 | + } |
| 81 | + |
| 82 | + if (null === $this->customerGroupId) { |
| 83 | + $this->customerGroupId = $this->getCustomerGroupId($context); |
| 84 | + } |
| 85 | + |
| 86 | + /** @var Product $product */ |
| 87 | + $product = $value['model']; |
| 88 | + $productId = $product->getId(); |
| 89 | + $this->productIds[] = $productId; |
| 90 | + $that = $this; |
| 91 | + |
| 92 | + return $this->valueFactory->create( |
| 93 | + function () use ($that, $productId, $context) { |
| 94 | + $tierPrices = []; |
| 95 | + if (empty($that->productIds)) { |
| 96 | + return []; |
| 97 | + } |
| 98 | + if (!$that->collection->isLoaded()) { |
| 99 | + $that->collection->addIdFilter($that->productIds); |
| 100 | + $that->collection->addTierPriceDataByGroupId($that->customerGroupId); |
| 101 | + } |
| 102 | + /** @var \Magento\Catalog\Model\Product $item */ |
| 103 | + foreach ($that->collection as $item) { |
| 104 | + if ($item->getId() === $productId) { |
| 105 | + // Try to extract all requested fields from the loaded collection data |
| 106 | + foreach ($item->getTierPrices() as $tierPrice) { |
| 107 | + $tierPrices[] = $tierPrice->getData(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return $tierPrices; |
| 112 | + } |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get the customer group Id. |
| 118 | + * |
| 119 | + * @param \Magento\GraphQl\Model\Query\ContextInterface $context |
| 120 | + * |
| 121 | + * @return int |
| 122 | + */ |
| 123 | + private function getCustomerGroupId(\Magento\GraphQl\Model\Query\ContextInterface $context) |
| 124 | + { |
| 125 | + $currentUserId = $context->getUserId(); |
| 126 | + if (!$currentUserId) { |
| 127 | + $customerGroupId = GroupManagement::NOT_LOGGED_IN_ID; |
| 128 | + } else { |
| 129 | + try { |
| 130 | + $customer = $this->customerRepository->getById($currentUserId); |
| 131 | + } catch (NoSuchEntityException $e) { |
| 132 | + throw new GraphQlNoSuchEntityException( |
| 133 | + __('Customer with id "%customer_id" does not exist.', ['customer_id' => $currentUserId]), |
| 134 | + $e |
| 135 | + ); |
| 136 | + } |
| 137 | + $customerGroupId = $customer->getGroupId(); |
| 138 | + } |
| 139 | + return $customerGroupId; |
| 140 | + } |
| 141 | +} |
0 commit comments