Magento how to programmatically subscribe


Step 1: Define Function


Magento 1


<?php class MageCheck_Tutorial_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { $this-> createSubscribe(); } public function createSubscribe() { // ... } }

Magento 2


/** * @var \Magento\Newsletter\Model\Subscriber */ protected $_subscriber; public function __construct( \Magento\Newsletter\Model\Subscriber $subscriber ){ $this->_subscriber = $subscriber; } public function execute() { $this->creatingSubscribe(); } public function creatingSubscribe() { // ... }

Step 2: Create Function


Magento 1


public function createSubscribe() { $customer = Mage::getModel('customer/customer') ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail('example@magecheck.com'); $customer->setConfirmation(true); $customer->save(); }

Magento 2


public function creatingSubscribe() { $email = 'example@magecheck.com'; $subscriber = $this->_subscriber; $subscriber->subscribe($email); $subscriber->save(); }