1

I have an existing customer id, i want to set the customer subscribed to the newsletter using this customer id, is there a way to do this?

  • Check my answer for check customer already subscribed and subscribe new customer by Id – Prince Patel Feb 15 '18 at 17:57
4

Notes: This answer may need to be changed in later version of Magento when service contract is implemented for the newsletter module. For now we will have to make do with using model method.

use Magento\Newsletter\Model\SubscriberFactory; // Inject SubscriberFactory
...
$subscriber = $this->subscriberFactory->create();
$subscriber->subscribeCustomerById($customerID);

Hope this helps.

  • how to check if customer already subscribed and how to unsubscribed customer? – Idham Choudry Feb 15 '18 at 3:13
  • The same model has some useful methods you can use for pretty much everything. For example, to check if customer has subscribed: ->loadByCustomerId(), then call $subscriber->isSubscribed(). To unsubscribe: $subscriber->unsubscribe() after you have loaded by customer id – William Tran Feb 15 '18 at 3:16
2
protected $_subscriberFactory;
protected $_customerRepositoryInterface;
protected $messageManager;

public function __construct(
    ...
    \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
    \Magento\Framework\Message\ManagerInterface $messageManager,
    ...
) {
    $this->_subscriberFactory = $subscriberFactory;
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
    $this->messageManager = $messageManager;
}

public function subscribeByCustomerId($customerId)
{
    $customer = $this->_customerRepositoryInterface->getById($customerId);
    $email = $customer->getEmail();

    $subscriber = $this->_subscriberFactory->create()->loadByEmail($email);
    if ($subscriber->getId()
        && $subscriber->getSubscriberStatus() == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED
    ) {
        $this->messageManager->addSuccess(__("This email address is already subscribed."));
    } else {
        $this->_subscriberFactory->create()->subscribe($email);
        $this->messageManager->addSuccess(__('Thank you for your subscription.'));
    }
}

Now call function subscribeByCustomerId() to subscribe new customer by Id

$customerId = 1;
$this->subscribeByCustomerId($customerId);
  • Why would you want to load by email when there's a $customerId already ? – William Tran Feb 16 '18 at 2:21
  • @WilliamTran You already given an answer. So I post something different. This method is used by core Magento. The reason behind using email to check customer is already subscribed or not with same subscriberFactory class. – Prince Patel Feb 16 '18 at 4:43
  • Make senses, I just want to know if subscribe($email) does anything that I wasn't aware of and should be used instead. – William Tran Feb 16 '18 at 4:47
  • @WilliamTran Check vendor/magento/module-newsletter/Controller/Subscriber/NewAction.php for more details without unwanted comments. :) – Prince Patel Feb 16 '18 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.