3
3
* Copyright 2025 Adobe
4
4
* All Rights Reserved.
5
5
*/
6
-
7
6
declare (strict_types=1 );
8
7
9
8
namespace Magento \Catalog \Plugin \Model ;
10
9
11
10
use Magento \Authorization \Model \UserContextInterface ;
12
- use Magento \Catalog \Model \ProductRenderList as Subject ;
11
+ use Magento \Catalog \Model \ProductRenderList ;
13
12
use Magento \Customer \Api \CustomerRepositoryInterface ;
14
13
use Magento \Customer \Model \Group ;
15
- use Magento \Customer \Model \Session as CustomerSession ;
14
+ use Magento \Customer \Model \Context ;
16
15
use Magento \Framework \Api \SearchCriteriaInterface ;
16
+ use Magento \Framework \App \Http \Context as HttpContext ;
17
17
use Magento \Framework \App \State ;
18
18
use Magento \Framework \Exception \LocalizedException ;
19
19
use Magento \Framework \Exception \NoSuchEntityException ;
20
- use Magento \Framework \Webapi \Rest \Request ;
21
20
use Psr \Log \LoggerInterface ;
22
21
23
22
/**
24
- * Plugin to fix customer group context in ProductRenderList for API requests
23
+ * Plugin to set customer group context for REST API pricing
25
24
*/
26
- class ProductRenderList
25
+ class ProductRenderListPlugin
27
26
{
28
27
/**
29
28
* @var UserContextInterface
@@ -36,9 +35,9 @@ class ProductRenderList
36
35
private $ customerRepository ;
37
36
38
37
/**
39
- * @var CustomerSession
38
+ * @var HttpContext
40
39
*/
41
- private $ customerSession ;
40
+ private $ httpContext ;
42
41
43
42
/**
44
43
* @var State
@@ -50,60 +49,57 @@ class ProductRenderList
50
49
*/
51
50
private $ logger ;
52
51
53
- /**
54
- * @var Request
55
- */
56
- private $ request ;
57
-
58
52
/**
59
53
* @param UserContextInterface $userContext
60
54
* @param CustomerRepositoryInterface $customerRepository
61
- * @param CustomerSession $customerSession
55
+ * @param HttpContext $httpContext
62
56
* @param State $appState
63
57
* @param LoggerInterface $logger
64
- * @param Request $request
65
58
*/
66
59
public function __construct (
67
60
UserContextInterface $ userContext ,
68
61
CustomerRepositoryInterface $ customerRepository ,
69
- CustomerSession $ customerSession ,
62
+ HttpContext $ httpContext ,
70
63
State $ appState ,
71
- LoggerInterface $ logger ,
72
- Request $ request
64
+ LoggerInterface $ logger
73
65
) {
74
66
$ this ->userContext = $ userContext ;
75
67
$ this ->customerRepository = $ customerRepository ;
76
- $ this ->customerSession = $ customerSession ;
68
+ $ this ->httpContext = $ httpContext ;
77
69
$ this ->appState = $ appState ;
78
70
$ this ->logger = $ logger ;
79
- $ this ->request = $ request ;
80
71
}
81
72
82
73
/**
83
- * Before getList - set customer group context for proper pricing
74
+ * Set customer group context in HTTP context for REST API requests
84
75
*
85
- * @param Subject $subject
76
+ * @param ProductRenderList $subject
86
77
* @param SearchCriteriaInterface $searchCriteria
87
- * @param int $storeId
88
- * @param string $currencyCode
78
+ * @param int|null $storeId
79
+ * @param string|null $currencyCode
89
80
* @return array
81
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
90
82
*/
91
83
public function beforeGetList (
92
- Subject $ subject ,
84
+ ProductRenderList $ subject ,
93
85
SearchCriteriaInterface $ searchCriteria ,
94
- $ storeId ,
95
- $ currencyCode
86
+ $ storeId = null ,
87
+ $ currencyCode = null
96
88
): array {
97
89
try {
98
- if ($ this ->appState ->getAreaCode () !== 'webapi_rest ' ) {
90
+ $ areaCode = $ this ->appState ->getAreaCode ();
91
+ if (!in_array ($ areaCode , ['webapi_rest ' , 'webapi_soap ' ], true )) {
99
92
return [$ searchCriteria , $ storeId , $ currencyCode ];
100
93
}
101
-
102
94
$ customerGroupId = $ this ->getCustomerGroupId ();
103
95
104
- // Set customer group ID in session for proper pricing context
105
96
if ($ customerGroupId !== null ) {
106
- $ this ->customerSession ->setCustomerGroupId ($ customerGroupId );
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
+ );
107
103
}
108
104
109
105
} catch (\Exception $ e ) {
@@ -117,7 +113,7 @@ public function beforeGetList(
117
113
}
118
114
119
115
/**
120
- * Get customer group ID from current context
116
+ * Get customer group ID from authenticated user context
121
117
*
122
118
* @return int|null
123
119
*/
@@ -137,14 +133,8 @@ private function getCustomerGroupId(): ?int
137
133
return Group::NOT_LOGGED_IN_ID ;
138
134
139
135
} catch (NoSuchEntityException $ e ) {
140
- $ this ->logger ->warning (
141
- 'Customer not found in ProductRenderList plugin: ' . $ e ->getMessage ()
142
- );
143
136
return Group::NOT_LOGGED_IN_ID ;
144
137
} catch (LocalizedException $ e ) {
145
- $ this ->logger ->error (
146
- 'Error getting customer group ID in ProductRenderList plugin: ' . $ e ->getMessage ()
147
- );
148
138
return null ;
149
139
}
150
140
}
0 commit comments