2

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?

1
  • Check my answer for check customer already subscribed and subscribe new customer by Id Feb 15, 2018 at 17:57

2 Answers 2

5

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.

2
  • how to check if customer already subscribed and how to unsubscribed customer? Feb 15, 2018 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 Feb 15, 2018 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);
4
  • Why would you want to load by email when there's a $customerId already ? Feb 16, 2018 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. Feb 16, 2018 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. Feb 16, 2018 at 4:47
  • @WilliamTran Check vendor/magento/module-newsletter/Controller/Subscriber/NewAction.php for more details without unwanted comments. :) Feb 16, 2018 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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