1

I have 2 events running if subscribe to newsletter is checked on the checkout page.

  1. sales_model_service_quote_submit_success
  2. newsletter_subscriber_save_after

Can i prevent event #2 from happening if event #1 has already ran?

2

This looks like a situation where you might want to store a variable that logs whether that first event has run or not.

If you have an observer for sales_model_service_quote_submit_success, you can make good use of the DataPersistorInterface class to store a temporary session variable that marks whether sales_model_service_quote_submit_success already ran.

So..

class QuoteSubmitSuccessObserver implements ObserverInterface
{
    protected $dataPersistor;

    public function __construct(\Magento\Framework\App\Request\DataPersistorInterface $dataPersistor) {
        $this->dataPersistor = $dataPersistor;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $this->dataPersistor->set('quote_submit_success', true)
    }
}

And

class NewsletterSubscriberSaveObserver implements ObserverInterface
{
    protected $dataPersistor;

    public function __construct(\Magento\Framework\App\Request\DataPersistorInterface $dataPersistor) {
        $this->dataPersistor = $dataPersistor;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        if (!$this->dataPersistor->get('quote_submit_success')) {
            … do something here … 
        }
    }
}
  • Thanks, this did the job for me, i just need to clear the session after reading it, i found this useful. $this->_logger->clear($key) – Kasper Morberg Bang Nov 28 '18 at 9:45

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.