|
7 | 7 |
|
8 | 8 | use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
|
9 | 9 | use Magento\Customer\Api\Data\CustomerInterface;
|
| 10 | +use Magento\Framework\Api\SearchCriteria; |
| 11 | +use Magento\Framework\Api\SearchResults; |
10 | 12 | use Magento\Newsletter\Model\SubscriberFactory;
|
11 | 13 | use Magento\Framework\Api\ExtensionAttributesFactory;
|
12 | 14 | use Magento\Newsletter\Model\ResourceModel\Subscriber;
|
@@ -165,6 +167,34 @@ public function afterGetById(CustomerRepository $subject, CustomerInterface $cus
|
165 | 167 | return $customer;
|
166 | 168 | }
|
167 | 169 |
|
| 170 | + /** |
| 171 | + * Plugin after getById customer that obtains newsletter subscription status for given customer. |
| 172 | + * |
| 173 | + * @param CustomerRepository $subject |
| 174 | + * @param SearchResults $searchResults |
| 175 | + * @param SearchCriteria $searchCriteria |
| 176 | + * @return SearchResults |
| 177 | + */ |
| 178 | + public function afterGetList(CustomerRepository $subject, SearchResults $searchResults, SearchCriteria $searchCriteria) |
| 179 | + { |
| 180 | + |
| 181 | + foreach ($searchResults->getItems() as $customer) { |
| 182 | + $extensionAttributes = $customer->getExtensionAttributes(); |
| 183 | + |
| 184 | + if ($extensionAttributes === null) { |
| 185 | + /** @var CustomerExtensionInterface $extensionAttributes */ |
| 186 | + $extensionAttributes = $this->extensionFactory->create(CustomerInterface::class); |
| 187 | + $customer->setExtensionAttributes($extensionAttributes); |
| 188 | + } |
| 189 | + if ($extensionAttributes->getIsSubscribed() === null) { |
| 190 | + $isSubscribed = $this->isSubscribed($customer); |
| 191 | + $extensionAttributes->setIsSubscribed($isSubscribed); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return $searchResults; |
| 196 | + } |
| 197 | + |
168 | 198 | /**
|
169 | 199 | * This method returns newsletters subscription status for given customer.
|
170 | 200 | *
|
|
0 commit comments