Skip to content

Commit 1be1204

Browse files
Merge pull request #10001 from magento-gl/spartans_pr_11082025_AC-5979
[Spartans] BugFixes Delivery
2 parents fe72c40 + 080c5c0 commit 1be1204

File tree

11 files changed

+1568
-4
lines changed

11 files changed

+1568
-4
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
* @var array Cache for customer group IDs
54+
*/
55+
private $customerGroupCache = [];
56+
57+
/**
58+
* @param UserContextInterface $userContext
59+
* @param CustomerRepositoryInterface $customerRepository
60+
* @param HttpContext $httpContext
61+
* @param State $appState
62+
* @param LoggerInterface $logger
63+
*/
64+
public function __construct(
65+
UserContextInterface $userContext,
66+
CustomerRepositoryInterface $customerRepository,
67+
HttpContext $httpContext,
68+
State $appState,
69+
LoggerInterface $logger
70+
) {
71+
$this->userContext = $userContext;
72+
$this->customerRepository = $customerRepository;
73+
$this->httpContext = $httpContext;
74+
$this->appState = $appState;
75+
$this->logger = $logger;
76+
}
77+
78+
/**
79+
* Set customer group context in HTTP context for REST API requests
80+
*
81+
* @param ProductRenderList $subject
82+
* @param SearchCriteriaInterface $searchCriteria
83+
* @param int|null $storeId
84+
* @param string|null $currencyCode
85+
* @return array
86+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
87+
*/
88+
public function beforeGetList(
89+
ProductRenderList $subject,
90+
SearchCriteriaInterface $searchCriteria,
91+
$storeId = null,
92+
$currencyCode = null
93+
): array {
94+
try {
95+
$areaCode = $this->appState->getAreaCode();
96+
if (!in_array($areaCode, ['webapi_rest', 'webapi_soap'], true)) {
97+
return [$searchCriteria, $storeId, $currencyCode];
98+
}
99+
$customerGroupId = $this->getCustomerGroupId();
100+
101+
if ($customerGroupId !== null) {
102+
// Set in HTTP context for cache and general context
103+
$this->httpContext->setValue(
104+
Context::CONTEXT_GROUP,
105+
(string)$customerGroupId,
106+
Group::NOT_LOGGED_IN_ID
107+
);
108+
}
109+
110+
} catch (\Exception $e) {
111+
$this->logger->error(
112+
'Error in ProductRenderList plugin: ' . $e->getMessage(),
113+
['exception' => $e]
114+
);
115+
}
116+
117+
return [$searchCriteria, $storeId, $currencyCode];
118+
}
119+
120+
/**
121+
* Get customer group ID from authenticated user context
122+
*
123+
* @return int|null
124+
*/
125+
private function getCustomerGroupId(): ?int
126+
{
127+
try {
128+
$userType = $this->userContext->getUserType();
129+
130+
if ($userType === UserContextInterface::USER_TYPE_CUSTOMER) {
131+
$customerId = $this->userContext->getUserId();
132+
if ($customerId) {
133+
if (!isset($this->customerGroupCache[$customerId])) {
134+
$customer = $this->customerRepository->getById($customerId);
135+
$this->customerGroupCache[$customerId] = (int)$customer->getGroupId();
136+
}
137+
return $this->customerGroupCache[$customerId];
138+
}
139+
}
140+
// For guest users, return the not logged in group ID
141+
return Group::NOT_LOGGED_IN_ID;
142+
143+
} catch (NoSuchEntityException $e) {
144+
return Group::NOT_LOGGED_IN_ID;
145+
} catch (LocalizedException $e) {
146+
return null;
147+
}
148+
}
149+
}

0 commit comments

Comments
 (0)