Skip to content

Commit c9f9d71

Browse files
improve fix
1 parent d867655 commit c9f9d71

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Newsletter\Model\SubscriptionManagerInterface;
1818
use Magento\Store\Model\Store;
1919
use Magento\Store\Model\StoreManagerInterface;
20+
use Magento\Framework\Api\SearchResults;
2021
use Psr\Log\LoggerInterface;
2122

2223
/**
@@ -229,6 +230,27 @@ public function afterGetById(CustomerRepositoryInterface $subject, CustomerInter
229230
return $customer;
230231
}
231232

233+
/**
234+
* Add subscription status to customer list
235+
*
236+
* @param CustomerRepositoryInterface $subject
237+
* @param SearchResults $searchResults
238+
* @return SearchResults
239+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
240+
*/
241+
public function afterGetList(CustomerRepositoryInterface $subject, SearchResults $searchResults): SearchResults
242+
{
243+
foreach ($searchResults->getItems() as $customer) {
244+
/** @var CustomerExtensionInterface $extensionAttributes */
245+
$extensionAttributes = $customer->getExtensionAttributes();
246+
247+
$isSubscribed = (int) $extensionAttributes->getIsSubscribed() === Subscriber::STATUS_SUBSCRIBED ?: false;
248+
$extensionAttributes->setIsSubscribed($isSubscribed);
249+
}
250+
251+
return $searchResults;
252+
}
253+
232254
/**
233255
* Set Is Subscribed extension attribute
234256
*

app/code/Magento/Newsletter/etc/extension_attributes.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
1010
<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
11-
<attribute code="is_subscribed" type="boolean" />
11+
<attribute code="is_subscribed" type="boolean" >
12+
<join reference_table="newsletter_subscriber" reference_field="customer_id" join_on_field="entity_id">
13+
<field>subscriber_status</field>
14+
</join>
15+
</attribute>
1216
</extension_attributes>
1317
</config>

0 commit comments

Comments
 (0)