Skip to main content

Magento 2 Check Is Customer Subscribed To Newsletter ?

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.
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

Popular posts from this blog

Magento 2 Set Title And Meta Title In Page Programatically

We can set different Meta Title and Page Heading programatically. I am giving you example. Refer below example - public function __construct ( \Magento\Framework\View\Element\Template\Context $context , \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig , \Magento\Framework\View\Page\Config $pageConfig , $data = array () ) { $this -> _scopeConfig = $scopeConfig ; $this -> _pageConfig = $pageConfig ; parent :: __construct ( $context , $data ); } /** * Prepare global layout * * @return $this */ protected function _prepareLayout () { $this -> _pageConfig -> addBodyClass ( 'advance-sitemap' ); if ( $this -> getSeoTitle ()) $this -> _pageConfig -> getTitle ()-> set ( 'Meta Title' ); if ( $this -> getMetaKeywords ()) $this -> _pageConfig -> setKeywords ( 'Meta Keywords' ); if ( $this -> getMetaDescription ()) $this -&g

Remove SID (session ID) from URL in Magento 2

Before removing SID (session ID) we should have knowledge why Magento uses SID (session ID) . Use SID on Frontend — Set to Yes if you want a user to stay logged in while switching between stores. Go to the Stores > Configuration > General > Web > Session Validation Settings > Use SID on Storefront and set its value to No   Thank You.!

Magento 2 Disable Minicart functionality Completely

The Cart Sidebar is often called the  mini cart  and displays a summary of the items in the cart. It is enabled by default and appears when you click the number of items in the Cart Link.   To Disable Mini cart Sidebar follow below steps - On the Admin sidebar, click  Stores . In the  Settings  section, choose  Configuration . In the  Sales  section on the left panel, choose  Checkout . Expand the  Shopping Cart Sidebar  section. Set  Display Shopping Cart Sidebar to No . Click  Save Config . Thank You.!