magento2: Newsletter subscription does not set the correct store_id if already subscribed. Not Fixed in 2.3-dev

Regarding https://github.com/magento/magento2/issues/19131. I (again) tried it on 2.3-dev an the issue is not fixed in 2.3-dev.

Preconditions (*)

  1. latest Magento Version 2.3-dev
  2. Have at least two stores ( A and B)

Steps to reproduce (*)

  1. Register a user in A
  2. Subscribe to Newsletter in A
  3. Switch to Store B
  4. Subscribe to Newsletter again

Expected result (*)

  1. When in store B, Subscriber::loadByEmail() should work 2. SELECT store_id, customer_id FROM newsletter_subscriber should look like this:
+----------+-------------+
| store_id | customer_id |
+----------+-------------+
|        1 |           1 |
|        2 |           1 |

Actual result (*)

  1. When in store B, loadByEmail() (or loadById() ) cant find the subscriber since
  2. SELECT store_id, customer_id FROM newsletter_subscriber looks like this:
+----------+-------------+
| store_id | customer_id |
+----------+-------------+
|        1 |           1 |
|        1 |           1 |

What i think where the error is

In app/code/Magento/Newsletter/Model/Subscriber.php starting on line 468: If the subscriber already exists with this email, the new entry gets the $customer->getStoreId() not the $this->_storeManager->getStore()->getId()

If this is the intended behavior then loadByEmail() or loadByCustomerId() should be store_id agnostic.

Cheers! Ben

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 20 (11 by maintainers)

Most upvoted comments

I also spot issue related to this and there is another way to reproduce it, in backend:

  1. Install clean Magento 2.2.x or 2.3.x (sample data not needed)
  2. Log in into admin backend
  3. Create a second website, store and store view
  4. Open the second website frontend
  5. Register new customer account and subscribe to the newsletter during registration
  6. Back to the backend
  7. Edit customer, open Newsletter tab. Note that he is not marked as subscribed
  8. Select newsletter checkbox to sign him up
  9. Click “Save and Continue Edit”

A success message is displayed, the customer is added to the newsletter_subscriber table, but the customer is still not marked as subscribed in the edit form. Doing steps 7 to 9 results in duplicating entry in subscribers table. After few saves I see following:

+---------------+----------+-------------+-------------------+
| subscriber_id | store_id | customer_id | subscriber_status |
+---------------+----------+-------------+-------------------+
|             3 |        2 |           3 |                 1 |
|             4 |        2 |           3 |                 1 |
|             5 |        2 |           3 |                 1 |
+---------------+----------+-------------+-------------------+

Additionally, it sends an email confirmation to the customer each time.

The same problem occurs when such customer is subscribed or saved by CLI or CRON job.

It looks it was introduced in the https://github.com/magento/magento2/commit/65000d81b388a7218ae9c9ec5c12bcb00d932c3c commit.

The problem doesn’t occur in 2.1.5 as loadByCustomerData method was ignoring store_id there.

Maybe \Magento\Newsletter\Model\Subscriber::loadByCustomerId shouldn’t overwrtite store_id on customer record? In my case it seems to fix the problem.

Another issue is that when customer for the new website is created by admin in the backend and he is subscribed to the newsletter by admin, store_id of his record in newsletter_subscriber is 1. This can lead to other buggy use cases.