how do I get subscriber information, base on the subscriber ID?
-
13-04-2021 - |
Pregunta
how do I get subscriber information, base on the subscriber ID? I would like to grab their email address, name ...etc by the subscriber ID. or even their customer ID
Thanks
Solución
check below code
Create Block
File:-app/code/[VendorName]/[ModuleName]/Block/SubscribeMagento.php
<?php
/**
* @copyright DeviMage
*
* @see PROJECT_LICENSE.txt
*/
namespace VendorName\ModuleName\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Newsletter\Model\SubscriberFactory;
/**
* Class SubscribeMagento
*/
class SubscribeMagento extends Template
{
/**
* @var SubscriberFactory
*/
private $subscriberFactory;
/**
* SubscribeMagento constructor.
* @param Context $context
* @param array $data
*/
public function __construct(
Context $context,
SubscriberFactory $subscriberFactory,
array $data = []
) {
parent::__construct($context, $data);
$this->subscriberFactory = $subscriberFactory;
}
public function getSubscribeDataById() {
$ID = 7;
try {
$collection = $this->subscriberFactory->create()->getCollection()
->addFieldToFilter('subscriber_id', $ID);
}catch (\Exception $e) {
return null;
}
return $collection;
}
}
Then print on template Customtemplate.phtml
echo '<PRE>';
print_r($block->getSubscribeDataById()->getData());
If directly using object manager to get subscribe data then check below code
Object Manger
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$subscribeCollection = $objectManager->create('Magento\Newsletter\Model\Subscriber');
/** Apply filters here */
$ID = 7;
$collection = $subscribeCollection->getCollection()
->addFieldToFilter('subscriber_id', $ID);
foreach ($collection as $subscribe){
echo 'Email = '.$subscribe->getSubscriberEmail().'<br>';
echo 'CustomerId = '.$subscribe->getCustomerId().'<br>';
echo 'SubscriberStatus = '.$subscribe->getSubscriberStatus().'<br>';
}
Then hit below commands
sudo rm -rf var/* generated/* pub/static/frontend/*
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:static-content:deploy -f
sudo php bin/magento ca:cl
sudo php bin/magento ca:fl
sudo chmod -R 777 /var/www/html/(project name)/
I hope this will help you..!
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange