0

I am trying to get the value of 'if newsletter subscribed' on customer save. I am using the event: customer_save_after_data_object for customer update and tried to get the subscription value by this piece code

protected $_subscriber;
public function __construct(
    \Magento\Newsletter\Model\Subscriber $subscriber
) {
    $this->_subscriber= $subscriber;
}
public function myUserDefinedFunction() {
    $checkSubscriber = $this->_subscriber->loadByEmail($customer->getEmail());
    $checkSubscriber->isSubscribed();
}

Though it is giving the before saved value for the subscription status. I've also tried by adding another event newsletter_subscriber_save_before, but no luck. Is there any other solution to get the desired result?

1 Answer 1

0

Try with below

/**
     * Subscriber factory
     *
     * @var SubscriberFactory
     */
    protected $_subscriberFactory;

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

public function myUserDefinedFunction() {
    $subscriber = $this->_subscriberFactory->create()->loadByEmail($email);
    if ($subscriber->getId(){
      echo __("This email address is already subscribed");
    }

}

[Update]

Change state of the subscription, right at the time the customer is saved from the admin end.

1. app/code/Stackexchange/HideShip/etc/adminhtml/events.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_save_after_data_object">
        <observer name="change_subscriber_state"
                  instance="Stackexchange\ModuleName\Observer\CustomereditPost"/>
    </event>
</config>

2. app/code/Stackexchange/HideShip/Observer/CustomereditPost.php

<?php

namespace Stackexchange\ModuleName\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\Newsletter\Model\Subscriber;

class CustomereditPost implements ObserverInterface
{
    protected $_subscriberFactory;

    public function __construct(
        SubscriberFactory $subscriberFactory
    )
    {
        $this->_subscriberFactory = $subscriberFactory;
    }

    public function execute(EventObserver $observer)
    {
        $email = "[email protected]";
        $subscriber = $this->_subscriberFactory->create()->loadByEmail($email);
        if ($subscriber->getId()) {
            echo __("This email address is already subscribed");
        } else {
                //$this->_subscriberFactory->create()->subscribe($email);
            $this->_subscriberFactory->create()
                ->setStatus(Subscriber::STATUS_SUBSCRIBED)
                ->setEmail($email)
                ->save();
        }
    }
}
4
  • It is giving the same result, i.e, the state of subscription before saving the customer. I require the changed state with every time the customer object is saved. For now, I've simply used $this->request->getParam('subscription') to get the current change of subscription with customer form submission
    – Anant
    Dec 19, 2019 at 11:59
  • This is the generic method to check whether a customer is subscribed or not. You can use that code based on your requirement. Still you have any query, update your question with other code as well. Dec 19, 2019 at 12:28
  • Your answer is correct to check if a customer is subscribed to the newsletter or not, at any random time. Though it is giving the same result as my code in the question is giving. I wanted the changed state of the subscription, right at the time the customer is saved from the admin end.
    – Anant
    Dec 20, 2019 at 4:30
  • Check my updates answer as well. If you need help, let me know. Dec 20, 2019 at 9:39

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.