33 * Copyright 2025 Adobe
44 * All Rights Reserved.
55 */
6-
76declare (strict_types=1 );
87
98namespace Magento \Catalog \Plugin \Model ;
109
1110use Magento \Authorization \Model \UserContextInterface ;
12- use Magento \Catalog \Model \ProductRenderList as Subject ;
11+ use Magento \Catalog \Model \ProductRenderList ;
1312use Magento \Customer \Api \CustomerRepositoryInterface ;
1413use Magento \Customer \Model \Group ;
15- use Magento \Customer \Model \Session as CustomerSession ;
14+ use Magento \Customer \Model \Context ;
1615use Magento \Framework \Api \SearchCriteriaInterface ;
16+ use Magento \Framework \App \Http \Context as HttpContext ;
1717use Magento \Framework \App \State ;
1818use Magento \Framework \Exception \LocalizedException ;
1919use Magento \Framework \Exception \NoSuchEntityException ;
20- use Magento \Framework \Webapi \Rest \Request ;
2120use Psr \Log \LoggerInterface ;
2221
2322/**
24- * Plugin to fix customer group context in ProductRenderList for API requests
23+ * Plugin to set customer group context for REST API pricing
2524 */
26- class ProductRenderList
25+ class ProductRenderListPlugin
2726{
2827 /**
2928 * @var UserContextInterface
@@ -36,9 +35,9 @@ class ProductRenderList
3635 private $ customerRepository ;
3736
3837 /**
39- * @var CustomerSession
38+ * @var HttpContext
4039 */
41- private $ customerSession ;
40+ private $ httpContext ;
4241
4342 /**
4443 * @var State
@@ -50,60 +49,57 @@ class ProductRenderList
5049 */
5150 private $ logger ;
5251
53- /**
54- * @var Request
55- */
56- private $ request ;
57-
5852 /**
5953 * @param UserContextInterface $userContext
6054 * @param CustomerRepositoryInterface $customerRepository
61- * @param CustomerSession $customerSession
55+ * @param HttpContext $httpContext
6256 * @param State $appState
6357 * @param LoggerInterface $logger
64- * @param Request $request
6558 */
6659 public function __construct (
6760 UserContextInterface $ userContext ,
6861 CustomerRepositoryInterface $ customerRepository ,
69- CustomerSession $ customerSession ,
62+ HttpContext $ httpContext ,
7063 State $ appState ,
71- LoggerInterface $ logger ,
72- Request $ request
64+ LoggerInterface $ logger
7365 ) {
7466 $ this ->userContext = $ userContext ;
7567 $ this ->customerRepository = $ customerRepository ;
76- $ this ->customerSession = $ customerSession ;
68+ $ this ->httpContext = $ httpContext ;
7769 $ this ->appState = $ appState ;
7870 $ this ->logger = $ logger ;
79- $ this ->request = $ request ;
8071 }
8172
8273 /**
83- * Before getList - set customer group context for proper pricing
74+ * Set customer group context in HTTP context for REST API requests
8475 *
85- * @param Subject $subject
76+ * @param ProductRenderList $subject
8677 * @param SearchCriteriaInterface $searchCriteria
87- * @param int $storeId
88- * @param string $currencyCode
78+ * @param int|null $storeId
79+ * @param string|null $currencyCode
8980 * @return array
81+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
9082 */
9183 public function beforeGetList (
92- Subject $ subject ,
84+ ProductRenderList $ subject ,
9385 SearchCriteriaInterface $ searchCriteria ,
94- $ storeId ,
95- $ currencyCode
86+ $ storeId = null ,
87+ $ currencyCode = null
9688 ): array {
9789 try {
98- if ($ this ->appState ->getAreaCode () !== 'webapi_rest ' ) {
90+ $ areaCode = $ this ->appState ->getAreaCode ();
91+ if (!in_array ($ areaCode , ['webapi_rest ' , 'webapi_soap ' ], true )) {
9992 return [$ searchCriteria , $ storeId , $ currencyCode ];
10093 }
101-
10294 $ customerGroupId = $ this ->getCustomerGroupId ();
10395
104- // Set customer group ID in session for proper pricing context
10596 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+ );
107103 }
108104
109105 } catch (\Exception $ e ) {
@@ -117,7 +113,7 @@ public function beforeGetList(
117113 }
118114
119115 /**
120- * Get customer group ID from current context
116+ * Get customer group ID from authenticated user context
121117 *
122118 * @return int|null
123119 */
@@ -137,14 +133,8 @@ private function getCustomerGroupId(): ?int
137133 return Group::NOT_LOGGED_IN_ID ;
138134
139135 } catch (NoSuchEntityException $ e ) {
140- $ this ->logger ->warning (
141- 'Customer not found in ProductRenderList plugin: ' . $ e ->getMessage ()
142- );
143136 return Group::NOT_LOGGED_IN_ID ;
144137 } catch (LocalizedException $ e ) {
145- $ this ->logger ->error (
146- 'Error getting customer group ID in ProductRenderList plugin: ' . $ e ->getMessage ()
147- );
148138 return null ;
149139 }
150140 }
0 commit comments