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