# Newsletter

The application uses Magento's newsletter system as the source of newsletter consent. The custom `Cru_Newsletter` module extends the standard feature with Cru-specific preferences and signup behaviour.

## Subscription flow

1. The storefront posts the email address to `Cru_Newsletter_SubscriberController::newAction()`.
2. The controller validates the registration token, email address and guest-subscription setting.
3. Magento creates or updates the `newsletter_subscriber` record.
4. The standard Magento statuses are used: subscribed, not active/unconfirmed and unsubscribed.
5. Cru preferences are saved for offers, investment research, portfolio communications and regional interests.
6. For a new guest email, the current implementation also creates a minimal Magento customer account and redirects the user to login.

The main newsletter consent signal is `newsletter_subscriber.subscriber_status`. Related communication preferences are stored as customer attributes, including:

- `cw_new_releases_email`
- `cw_sub_inv_resrch`
- `cw_unsub_portfolio`
- `cw_pref_region`

## Admin management

The custom **Customer Email Subscriptions** grid combines customer records with `newsletter_subscriber`. It shows customer identity, sales rep, customer type, newsletter status, investment research, portfolio valuations and regional preferences.

Saving its edit form updates both the customer attributes and the Magento newsletter subscriber status. A subscriber change also triggers the Dotdigital newsletter observer, which marks the connector contact for import or queues an unsubscribe/resubscribe operation.

## Scheduled task

`cru_dotmailer_refresh_segments` runs daily at 02:49 and refreshes the local `email_dm_segments` data. The effective time depends on the server and Magento cron timezone configuration.

## Troubleshooting

1. Check the customer's `newsletter_subscriber` record and `subscriber_status`.
2. Check the customer's related communication-preference attributes.
3. Confirm the correct website/store was used when the subscription was created.
4. Check whether guest subscriptions are enabled when troubleshooting a guest signup.
5. If Dotdigital is out of sync, verify the contact-sync and importer jobs described in [dotmailer-dotdigital.md](dotmailer-dotdigital.md).

## Key implementation files

Paths are relative to `web-root/`.

- `app/code/local/Cru/Newsletter/controllers/SubscriberController.php`
- `app/code/local/Cru/Newsletter/Model/Observer.php`
- `app/code/local/Cru/Newsletter/etc/config.xml`
- `app/code/local/Cru/Customer/controllers/Adminhtml/EmailsubscriptionsController.php`
- `app/code/local/Cru/Customer/Block/Adminhtml/Emailsubscriptions/Grid.php`
- `app/code/community/Dotdigitalgroup/Email/Model/Newsletter/Observer.php`
