Skip to content

Add afterGetList method in CustomerRepository plugin to retrieve is_s… #25311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
namespace Magento\Newsletter\Model\Plugin;

use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
use Magento\Customer\Api\Data\CustomerExtensionInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\Framework\Api\ExtensionAttributesFactory;
use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchResults;
use Magento\Newsletter\Model\ResourceModel\Subscriber;
use Magento\Customer\Api\Data\CustomerExtensionInterface;
use Magento\Newsletter\Model\SubscriberFactory;

class CustomerPlugin
{
Expand Down Expand Up @@ -165,6 +168,38 @@ public function afterGetById(CustomerRepository $subject, CustomerInterface $cus
return $customer;
}

/**
* Plugin after getById customer that obtains newsletter subscription status for given customer.
*
* @param CustomerRepository $subject
* @param SearchResults $searchResults
* @param SearchCriteria $searchCriteria
* @return SearchResults
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetList(
CustomerRepository $subject,
SearchResults $searchResults,
SearchCriteriaInterface $searchCriteria
) {

foreach ($searchResults->getItems() as $customer) {
$extensionAttributes = $customer->getExtensionAttributes();

if ($extensionAttributes === null) {
/** @var CustomerExtensionInterface $extensionAttributes */
$extensionAttributes = $this->extensionFactory->create(CustomerInterface::class);
$customer->setExtensionAttributes($extensionAttributes);
}
if ($extensionAttributes->getIsSubscribed() === null) {
$isSubscribed = $this->isSubscribed($customer);
$extensionAttributes->setIsSubscribed($isSubscribed);
}
}

return $searchResults;
}

/**
* This method returns newsletters subscription status for given customer.
*
Expand Down