How to get subscribers collection in Magento 2.3
-
15-04-2021 - |
Question
I'm trying to get the collection of subscribers but can't find how to do it in Magento 2, the ones I found were about 1.9. Is there a way to get it just like the users?
I've tried
<?php
namespace vendor\module\file;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
class Export extends Action
{
public function __construct(
Context $context,
\Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollection
) {
$this->subscriberCollection = $subscriberCollection;
parent::__construct($context);
}
public function execute()
{
$subscribers = $this->subscriberCollection->create()->getCollection();
}
}
But it doesn't seems to be like this.
Best regards
Solution
Try this,
<?php
namespace vendor\module\file;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
class Export extends Action
{
public function __construct(
Context $context,
\Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollection
) {
$this->subscriberCollection = $subscriberCollection;
parent::__construct($context);
}
public function execute()
{
$subscribers = $this->subscriberCollection->create();
foreach ($subscribers as $subscriber) {
print_r($subscriber->getData());
}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange