Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email subject does not respect user's preferred language in abandoned cart emails #92

Open
romainpoirier opened this issue Jan 21, 2025 · 5 comments

Comments

@romainpoirier
Copy link

romainpoirier commented Jan 21, 2025

Describe the bug

The email subject line for abandoned cart reminders does not respect the user's preferred language when it is set using a custom field. While I am able to dynamically set the email's content language using the Carts::EVENT_BEFORE_MAIL_SEND event, the subject line always defaults to the primary site language.

Context:

In my project, users have a custom field preferredLanguage, which specifies their preferred communication language. Using the following event listener, I dynamically set the email content to match this preference:

Event::on(
    Carts::class,
    Carts::EVENT_BEFORE_MAIL_SEND,
    function (BeforeMailSend $event) {
        $order = $event->order;
        $message = $event->message;

        // Retrieve the user associated with the order
        $user = User::find()->id($order->customerId)->one();

        if ($user) {
            // Get the user's preferred language (custom field)
            $preferredLanguage = $user->getFieldValue('preferredLanguage');

            // If no preferred language, fallback to the primary site language
            if (!$preferredLanguage) {
                $preferredLanguage = Craft::$app->sites->primarySite->language;
            }

            // Set the system language
            Craft::$app->language = $preferredLanguage;

            // Set the email message language
            $message->language = Localization::normalizeLanguage($preferredLanguage);
        }
    }
);

While this correctly updates the language for the email content, the subject lines configured in the plugin, such as:

'firstReminderSubject' => Craft::t('site', 'Votre panier vous attend'),
'secondReminderSubject' => Craft::t('site', 'Votre panier vous attend toujours'),

always fall back to the site's default language.

Expected:

The email subject should respect the user's preferred language.

Actual:

The email subject defaults to the site's primary language, regardless of the user's preference.

Additional Notes:

I suspect this behavior is related to how the subject is defined in the sendMail function of the plugin. It does not appear to take into account the dynamically set Craft::$app->language or the $message->language value.

Steps to reproduce

  1. Configure a Craft CMS project with the verbb/abandoned-cart plugin.
  2. Add a custom field preferredLanguage to the User model and set values for different users (e.g., "fr", "en").
  3. Create translations for email subjects in translations/{language}/site.php.
  4. Use the Carts::EVENT_BEFORE_MAIL_SEND event to set the email content's language dynamically based on the user's preferredLanguage field.
  5. Trigger an abandoned cart email for a user whose preferred language is different from the site's default language.

Craft CMS version

5.5.10

Plugin version

4.0.4

Multi-site?

Yes

Additional context

No response

@romainpoirier romainpoirier changed the title Email Subject Does Not Respect User's Preferred Language in Abandoned Cart Emails Email subject does not respect user's preferred language in abandoned cart emails Jan 21, 2025
@engram-design
Copy link
Member

That's very likely correct, as we're just using these values from the plugin settings. I think we can sort that out.

@romainpoirier
Copy link
Author

Thank you! In the meantime, while waiting for an improvement on this, is there a workaround available today to ensure that the subject of the sent email is in the user's preferred language?

@engram-design
Copy link
Member

I don't think there's a quick fix for this that's a DIY solution, sorry!

@romainpoirier
Copy link
Author

Wouldn't it be possible to modify the subject on the fly using verbb\abandonedcart\services\Carts\Carts::EVENT_BEFORE_MAIL_SEND to work around this?

Example:

Event::on(
    Carts::class,
    Carts::EVENT_BEFORE_MAIL_SEND,
    function (BeforeMailSend $event) {
        $message = $event->message;
        $message->subject = 'Custom translated text from user preferred language';
    }
);

@engram-design
Copy link
Member

Ah, you know I did forget about that event. You'd be right. I was thinking you could only use the native Craft EVENT_BEFORE_MAIL_SEND which isn't ideal, as there's no way to easily tell in that event what an email from Abandoned Carts is.

Thanks for raising that. Haven't tested it specifically, but I believe that should work for now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants