Skip to content

Commit 64d3b9c

Browse files
committed
Merge branch 'AC-5979' into spartans_pr_11082025_AC-5979
2 parents d15c409 + ccaec9e commit 64d3b9c

File tree

11 files changed

+1546
-4
lines changed

11 files changed

+1546
-4
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Plugin\Model;
9+
10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Catalog\Model\ProductRenderList;
12+
use Magento\Customer\Api\CustomerRepositoryInterface;
13+
use Magento\Customer\Model\Group;
14+
use Magento\Customer\Model\Context;
15+
use Magento\Framework\Api\SearchCriteriaInterface;
16+
use Magento\Framework\App\Http\Context as HttpContext;
17+
use Magento\Framework\App\State;
18+
use Magento\Framework\Exception\LocalizedException;
19+
use Magento\Framework\Exception\NoSuchEntityException;
20+
use Psr\Log\LoggerInterface;
21+
22+
/**
23+
* Plugin to set customer group context for REST API pricing
24+
*/
25+
class ProductRenderListPlugin
26+
{
27+
/**
28+
* @var UserContextInterface
29+
*/
30+
private $userContext;
31+
32+
/**
33+
* @var CustomerRepositoryInterface
34+
*/
35+
private $customerRepository;
36+
37+
/**
38+
* @var HttpContext
39+
*/
40+
private $httpContext;
41+
42+
/**
43+
* @var State
44+
*/
45+
private $appState;
46+
47+
/**
48+
* @var LoggerInterface
49+
*/
50+
private $logger;
51+
52+
/**
53+
* @param UserContextInterface $userContext
54+
* @param CustomerRepositoryInterface $customerRepository
55+
* @param HttpContext $httpContext
56+
* @param State $appState
57+
* @param LoggerInterface $logger
58+
*/
59+
public function __construct(
60+
UserContextInterface $userContext,
61+
CustomerRepositoryInterface $customerRepository,
62+
HttpContext $httpContext,
63+
State $appState,
64+
LoggerInterface $logger
65+
) {
66+
$this->userContext = $userContext;
67+
$this->customerRepository = $customerRepository;
68+
$this->httpContext = $httpContext;
69+
$this->appState = $appState;
70+
$this->logger = $logger;
71+
}
72+
73+
/**
74+
* Set customer group context in HTTP context for REST API requests
75+
*
76+
* @param ProductRenderList $subject
77+
* @param SearchCriteriaInterface $searchCriteria
78+
* @param int|null $storeId
79+
* @param string|null $currencyCode
80+
* @return array
81+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
82+
*/
83+
public function beforeGetList(
84+
ProductRenderList $subject,
85+
SearchCriteriaInterface $searchCriteria,
86+
$storeId = null,
87+
$currencyCode = null
88+
): array {
89+
try {
90+
$areaCode = $this->appState->getAreaCode();
91+
if (!in_array($areaCode, ['webapi_rest', 'webapi_soap'], true)) {
92+
return [$searchCriteria, $storeId, $currencyCode];
93+
}
94+
$customerGroupId = $this->getCustomerGroupId();
95+
96+
if ($customerGroupId !== null) {
97+
// Set in HTTP context for cache and general context
98+
$this->httpContext->setValue(
99+
Context::CONTEXT_GROUP,
100+
(string)$customerGroupId,
101+
Group::NOT_LOGGED_IN_ID
102+
);
103+
}
104+
105+
} catch (\Exception $e) {
106+
$this->logger->error(
107+
'Error in ProductRenderList plugin: ' . $e->getMessage(),
108+
['exception' => $e]
109+
);
110+
}
111+
112+
return [$searchCriteria, $storeId, $currencyCode];
113+
}
114+
115+
/**
116+
* Get customer group ID from authenticated user context
117+
*
118+
* @return int|null
119+
*/
120+
private function getCustomerGroupId(): ?int
121+
{
122+
try {
123+
$userType = $this->userContext->getUserType();
124+
125+
if ($userType === UserContextInterface::USER_TYPE_CUSTOMER) {
126+
$customerId = $this->userContext->getUserId();
127+
if ($customerId) {
128+
$customer = $this->customerRepository->getById($customerId);
129+
return (int)$customer->getGroupId();
130+
}
131+
}
132+
// For guest users, return the not logged in group ID
133+
return Group::NOT_LOGGED_IN_ID;
134+
135+
} catch (NoSuchEntityException $e) {
136+
return Group::NOT_LOGGED_IN_ID;
137+
} catch (LocalizedException $e) {
138+
return null;
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)