6

How to create newsletter subscriber programmatically ? I have email id I want to save that email id in newsletter subscriber how can I do that ?

<?php

/**
 * Copyright 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace vendorname\moduelname\Model;

use Sapient\CustomApi\Api\CustomInterface;

/**
 * Defines the implementaiton class of the calculator service contract.
 */
class Custom implements CustomInterface
{

    protected $subscriberFactory;


    public function __construct(

    \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
) {
    $this->subscriberFactory= $subscriberFactory;

}
    /**
     * Return the sum of the two numbers.
     *
     * @api
     * @param int $num1 Left hand operand.
     * @param int $num2 Right hand operand.
     * @return int The sum of the two values.
     */
    public function add($num1, $num2) {
        return $num1 + $num2;
    }

    /**
     * Sum an array of numbers.
     *
     * @api
     * @param string[] $email The array of numbers to sum.
     * @return string The sum of the numbers.
     */
    public function newsletter($email) {

           //$data ="sssssssss"; 
          // print_r($data);
           //die;
        $this->subscriberFactory->create()->subscribe($email);
        return $email;
    }
}

3 Answers 3

12

To do that you can inject Magento\Newsletter\Model\SubscriberFactory in your constructor:

protected $subscriberFactory;

public function __construct(
    //
    \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
) {
    $this->subscriberFactory= $subscriberFactory;
    //
}

Then in your code you can call:

$this->subscriberFactory->create()->subscribe($email);
5
  • i updated my question with code i want to subscribe under newsletter function but i am getting error Dec 14, 2016 at 11:25
  • How to subscribe first name ans last name? Sep 7, 2018 at 14:17
  • Above code works but firstname and lastname values how we can pass? Sep 10, 2018 at 7:51
  • @jafarpinjar the name of the subscriber is taken from the customer entity if one exists with that e-mail. Out of the box there's no way to set name values for guest customers as far as I can see. Sep 13, 2018 at 1:09
  • can we not override and customize it? Sep 14, 2018 at 6:08
2
$email = 'test_email@gmail.com';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$subscriber= $objectManager->create('Magento\Newsletter\Model\SubscriberFactory'); 
$subscriber->create()->subscribe($email);
3
0

From Magento 2.4, store_id has to be provided. You have to use SubscriptionManagerInterface class.

use Magento\Newsletter\Model\SubscriptionManagerInterface;

/**
 * @var SubscriptionManagerInterface
 */
private SubscriptionManagerInterface $subscriptionManager;

/* @param SubscriptionManagerInterface $subscriptionManager */
public function __construct(
    SubscriptionManagerInterface $subscriptionManager
) 
{
    $this->subscriptionManager= $subscriptionManager;
}

And then in your function:

$this->subscriptionManager->subscribeCustomer((int)$customer->getId(), (int)$storeId);

Your Answer

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

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