Magento 2 comes with many features and functionalities towards eCommerce. Newsletter is efficient way to aware for products and use brings trust to your business for your customers.
We will discuss how we can check customer is subscribed or not in newsletter functionality.
First of all we will inject the \Magento\Newsletter\Model\Subscriber class in constructor as below.
There are two possible cases which we can use to check customer is subscribed to newsletter or not.
Thank You.!
We will discuss how we can check customer is subscribed or not in newsletter functionality.
First of all we will inject the \Magento\Newsletter\Model\Subscriber class in constructor as below.
protected $_subscriber;
public function __construct(
...
\Magento\Newsletter\Model\Subscriber $subscriber
...
){
...
$this->_subscriber= $subscriber;
...
}
There are two possible cases which we can use to check customer is subscribed to newsletter or not.
Using Customer Email Address.
$checkSubscriber = $this->_subscriber->loadByEmail($customerEmail);
if ($checkSubscriber->isSubscribed()) {
// Customer is subscribed
} else {
// Customer is not subscribed
}
Using Customer Id.
$checkSubscriber = $this->_subscriber->loadByCustomerId($customerId);
if ($checkSubscriber->isSubscribed()) {
// Customer is subscribed
} else {
// Customer is not subscribed
}
Thank You.!
Comments
Post a Comment