Skip to content

Commit ba74b25

Browse files
author
Oleksandr Iegorov
committed
MC-38995: Customer group price is not working in product query graphql
1 parent 2cf0433 commit ba74b25

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\CustomerGraphQl\Model\Context;
9+
10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\GraphQl\Model\Query\ContextParametersInterface;
12+
use Magento\GraphQl\Model\Query\ContextParametersProcessorInterface;
13+
use Magento\Customer\Model\Session as CustomerSession;
14+
use Magento\Customer\Model\Group;
15+
use Magento\Customer\Api\CustomerRepositoryInterface;
16+
use Magento\Framework\Exception\LocalizedException;
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
class AddCustomerGroupToContext implements ContextParametersProcessorInterface
22+
{
23+
/**
24+
* @var CustomerSession
25+
*/
26+
private $customerSession;
27+
28+
/**
29+
* @var CustomerRepositoryInterface
30+
*/
31+
private $customerRepository;
32+
33+
/**
34+
* @param CustomerSession $customerSession
35+
* @param CustomerRepositoryInterface $customerRepository
36+
*/
37+
public function __construct(
38+
CustomerSession $customerSession,
39+
CustomerRepositoryInterface $customerRepository
40+
) {
41+
$this->customerSession = $customerSession;
42+
$this->customerRepository = $customerRepository;
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function execute(ContextParametersInterface $contextParameters): ContextParametersInterface
49+
{
50+
$customerSession = $this->customerSession;
51+
$customerGroupId = null;
52+
if ($contextParameters->getUserType() === UserContextInterface::USER_TYPE_GUEST) {
53+
$customerGroupId = Group::NOT_LOGGED_IN_ID;
54+
} elseif ($contextParameters->getExtensionAttributes()->getIsCustomer() === true) {
55+
try {
56+
$customer = $this->customerRepository->getById($contextParameters->getUserId());
57+
$customerGroupId = (int) $customer->getGroupId();
58+
} catch (LocalizedException $e) {
59+
$customerGroupId = null;
60+
}
61+
}
62+
if ($customerGroupId !== null) {
63+
$customerSession->setCustomerGroupId($customerGroupId);
64+
$contextParameters->addExtensionAttribute('customer_group_id', $customerGroupId);
65+
}
66+
return $contextParameters;
67+
}
68+
}

app/code/Magento/CustomerGraphQl/etc/graphql/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<arguments>
1818
<argument name="contextParametersProcessors" xsi:type="array">
1919
<item name="add_user_info_to_context" xsi:type="object">Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext</item>
20+
<item name="add_customer_group_to_context" xsi:type="object">Magento\CustomerGraphQl\Model\Context\AddCustomerGroupToContext</item>
2021
</argument>
2122
</arguments>
2223
</type>

0 commit comments

Comments
 (0)