0

I have a magento 2.2.9 CE based site and have multi-store views. My issue is when someone subscribes to the newsletter in one store view if they go in the future to another store view and try to subscribe they get an error saying they are already subscribed. So I want to allow email addresses to subscribe to each store view independently. Below is my code however it does not seem to work.

I don't see any errors it just gives me still the default already subscribed message.

 /**
 * New subscription action
 *
 * @return void
 */
public function execute()
{
    if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
        $email = (string)$this->getRequest()->getPost('email');
        //$storeview = (string)$this->getRequest()->getPost('storeview');
        $storeview ='store-a'; //hardcode store view for testing

        try {
            $this->validateEmailFormat($email);
            $this->validateGuestSubscription();
            $this->validateEmailAvailable($email);

            $subscriber = $this->_subscriberFactory->create()->loadByEmail($email);
            if ($subscriber->getId()
                && (int) $subscriber->getSubscriberStatus() === Subscriber::STATUS_SUBSCRIBED 
                && $subscriber->getSubscriberStoreView() === $storeview;

            ) {
                throw new LocalizedException(
                    __('Email address already subscribed to this brand.')
                );
            }

            $status = (int) $this->_subscriberFactory->create()->subscribe($email);
            $this->messageManager->addSuccessMessage($this->getSuccessMessage($status));
        } catch (LocalizedException $e) {
            $this->messageManager->addExceptionMessage(
                $e,
                __('There was a problem with the subscription: %1', $e->getMessage())
            );
        } catch (\Exception $e) {
            $this->messageManager->addExceptionMessage($e, __('Something went wrong with the subscription.'));
        }
    }
    $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
}

/**
  • Do you check what returns this? $subscriber->getSubscriberStoreView(), in every case this will return null Try to check store_id, something like this: $storeId = 1; if ($subscriber->getId() && (int) $subscriber->getSubscriberStatus() === Subscriber::STATUS_SUBSCRIBED && $subscriber->getStoreId() == $storeId ) { – Bashev Jan 18 at 7:34

Your Answer

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

Browse other questions tagged or ask your own question.